我使用以下代码对 JTable 列进行编辑/取消编辑,但是当用户重新排列列时,以下代码不起作用 SSCCE的代码如下:
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Main {
public static void main(String[] argv) throws Exception {
TableModel model = new DefaultTableModel() {
public boolean isCellEditable(int rowIndex, int mColIndex) {
boolean flag = false;
if (isEdit == true) {
if ((vColIndex == tblItem.getColumn("Design").getModelIndex())
|| (vColIndex == tblItem.getColumn("ChangedCategory").getModelIndex())
|| (vColIndex == tblItem.getColumn("Amount").getModelIndex())) {
flag = false;
} else {
flag = true;
}
} else {
flag = false;
}
return flag;
}
};
JTable table2 = new JTable(model);
}
}
最佳答案
请注意,模型索引和 View 索引不等效。如前所述here ,
JTable
provides methods that convert from model coordinates to view coordinates —convertColumnIndexToView
andconvertRowIndexToView
— and that convert from view coordinates to model coordinates —convertColumnIndexToModel
andconvertRowIndexToModel
.
教程部分讨论 Sorting and Filtering行,但该原则也适用于列。缺少complete example ,很难确定。
关于java - JTable 编辑/取消编辑代码不适用于重新排列的 JTable 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16186113/