The JTable Edit/UnEdit Code Not Working For Re Arranged JTable Columns

喜你入骨 提交于 2020-01-11 12:52:00

问题


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 and convertRowIndexToView — and that convert from view coordinates to model coordinates — convertColumnIndexToModel and convertRowIndexToModel.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!