5

我在 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);
4

4 回答 4

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);
  }

你应该有一个数组来保存更改

于 2013-04-15T05:27:06.137 回答
1

isCellEditable()在匿名内部类中添加函数AbstractTableModel

public boolean isCellEditable(int row, int col) { 
    return true; 
}
于 2013-04-15T05:29:00.590 回答
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;
                }
};
于 2013-04-15T05:29:08.630 回答
0

将 isCellEditable() 添加到您希望它们可编辑的行和列中,例如,如果您不希望某些列(如 ID)可编辑,则返回 false。请记住,您需要将编辑数据保存在某个位置

  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
}
于 2013-04-16T11:31:26.883 回答