import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
class ColorTableModel extends AbstractTableModel {
Object rowData[][] = { { "value1", Boolean.FALSE },
{ "value1", Boolean.FALSE }, { "value1", Boolean.FALSE },
{ "value1", Boolean.FALSE}, { "value1", Boolean.FALSE }, };
String columnNames[] = { "English", "Boolean" };
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int column) {
return columnNames[column];
}
public int getRowCount() {
return rowData.length;
}
public Object getValueAt(int row, int column) {
return rowData[row][column];
}
public Class getColumnClass(int column) {
return (getValueAt(0, column).getClass());
}
public void setValueAt(Object value, int row, int column) {
rowData[row][column] = value;
}
public boolean isCellEditable(int row, int column) {
return (column != 0);
}
}
public class EditableColorColumn {
public static void main(String args[]) {
JFrame frame = new JFrame("Editable Color Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new ColorTableModel();
JTable table = new JTable(model);
// TableColumn column = table.getColumnModel().getColumn(3);
// column.setCellRenderer(renderer);
// column.setCellEditor(editor);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
我想獲得第一列的值,如果我勾選相應的行。我已經閱讀了很多關於這方面的內容,但我不能把它寫入代碼。你可以編碼一個嗎?JTable Boolean.class
示例場景,當我選中row1
複選框時,它將system.out.println()
結果爲value1
。
你有一些代碼片段? – Jong
int row = table.getSelectedRow(); String value = table.getValueAt(row,0).toString(); – Addict
出現錯誤,應在何處放置代碼? – Jong