本文介绍了JTable Edit / UnEdit代码不适用于重新安排的JTable列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JTable Columns的Edit / UnEdit中使用以下代码,但是当用户重新排列列时,以下代码无效
代码的SSCCE如下:

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


推荐答案

注意该模型和视图索引等效。如所述,

Note that model and view indexes are not equivalent. As noted here,

教程部分讨论行,但该原则也适用于列。如果没有,则很难确定。

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.

这篇关于JTable Edit / UnEdit代码不适用于重新安排的JTable列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:40