0

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);
}
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, with hardcoded value for `JTable/XxxTableModel` stored as `local variable` – mKorbel Apr 24 '13 at 07:44
  • @mKorbel i have edited posted SSCCE. – Syed Muhammad Mubashir Apr 24 '13 at 08:00
  • `i applied your solution but it is not working`, not working almost in cases, disclaimer was described in my 2nd. answer (both I deleted) now we are returns from the end back to start, reason for asked SSCCE, I'm out of this thread – mKorbel Apr 24 '13 at 11:21
  • deleted posts and answers are visible for all users with reputations points >10k – mKorbel Apr 24 '13 at 11:29
  • @SyedMuhammadMubashir: You may need to convert model/view indexes, as suggested [here](http://stackoverflow.com/a/16191431/230513). – trashgod Apr 24 '13 at 12:00

1 Answers1

2

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045