Frage

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);
}
}
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top