我在java中使用JTable,但它不允许我编辑单元格。
private final TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return 5;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int row, int col) {
return new Integer(row*col);
}
};
private final JTable table = new JTable(dataModel);
答案 0 :(得分:9)
添加以下代码
public boolean isCellEditable(int row, int col)
{ return true; }
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
你应该有一个数组,你将保存更改
答案 1 :(得分:1)
在匿名内部类isCellEditable()
AbstractTableModel
函数
public boolean isCellEditable(int row, int col) {
return true;
}
答案 2 :(得分:1)
尝试
private final TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() {
return 5;
}
public int getRowCount() {
return 10;
}
public Object getValueAt(int row, int col) {
return new Integer(row*col);
}
public boolean isCellEditable(int row, int col) {
return true;
}
};
答案 3 :(得分:0)
将isCellEditable()添加到您希望它们可编辑的行和列中,例如,如果您不希望某些列(如ID)可编辑,则返回false。请记住,您需要将editit数据保存在某处
public boolean isCellEditable(int row, int col) {
return true; // or false for none editable columns
}
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value; // save edits some where
fireTableCellUpdated(row, col); // informe any object about changes
}