我有一个包含 7 列的 JTable,其中第六列是 boolean 值(复选框)。我想仅当检查第六列中的 boolean 值时才使第七列的单元格变得可编辑。例如,如果选中 (2,6),我希望仅 (2,7) 变得可编辑。
以下代码使整行可编辑(但我希望单元格仅位于 boolean 值右侧):
@Override
public boolean isCellEditable(int row, int col) {
if (col == 5) // 0-indexed (6th col)
return true;
else
return ((Boolean) getValueAt(row, 5)).booleanValue();
}
之后,我想要一种快速获取所有选中行的方法,这样我就可以获得每行第 7 列的值。
使用 Guido 的输入更新:代码有效,但当我单击第七列的不可编辑单元格时(无论一组是否可编辑),我收到 NullPointerException。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ManagerDisplay$1.isCellEditable(ManagerDisplay.java:82)
at javax.swing.JTable.editCellAt(JTable.java:3500)
最佳答案
如果我理解正确的话,你只需要这个:
@Override
public boolean isCellEditable(int row, int col) {
if (col == 5) // 0-indexed (6th col)
return true;
else if (col == 6) // 7th col
return ((Boolean) getValueAt(row, 5)).booleanValue();
else // every other column
return false;
}
关于java - 基于复选框 JTable 使特定单元格可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29045108/