I’m using a JTable, and I need to allow moving of its columns. But there comes the problem. Once they have rearranged, it’s like just they have changed there content and title only. But I also need their indexes to be changed as well.
To make this more clear, I’ll give an example. Let’s say my first column is called “names”. When calling the ‘setName()’ method, a name must always go to that column, no matter where the column is. But not to the first column..Hope you got my point!
I’ve finished writing required methods. Hope there is a way to achieve this without touching the finished methods.
Any help is much appreciated!!!
If you want to change the data, simply alter the underlying
TableModeldirectly. The order of the columns never changes in theTableModel, it is only in the view (theJTable) that those columns are re-ordered.If you do want to go through the table, you will have to convert from ‘view-coordinates’ to ‘model-coordinates’ by using the available
JTable#convertColumnIndexToModel(view -> model) and theJTable#convertColumnIndexToView(model->view) methods as @MadProgrammer mentioned.You should certainly not update the
setValueAtmethod of theTableModelas you mentioned in one of your comments. As said before, theTableModeldoes not change the order of the columns, so thesetValueAtshould always be called with ‘model coordinates’, hence there is no need to transform those