问题
I am using the following code for Edit/UnEdit for my JTable Columns, but when the user re-arranged the columns the following code is not working SSCCE of the code is following:
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);
}
}
回答1:
Note that model and view indexes are not equivalent. As noted 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
.
The tutorial section discusses Sorting and Filtering rows, but the principle applies to columns as well. Absent a complete example, it's hard to be sure.
来源:https://stackoverflow.com/questions/16186113/the-jtable-edit-unedit-code-not-working-for-re-arranged-jtable-columns