There is a JTable with columns in predifined order. Like
+----------------------+
|id | name |age |
I want to move columns in the order (for example name=0, age=1, id=2).
+----------------------+
|name | age | id |
How can i achive that? JTable has method moveColumns, but unfortunately this is not enough. Assume order is strored in map:.
Map<String, Integer> columnOrder = getOrder();
for(Map.Entry<String, Integer> e : columnOrder.entrySet()) {
int columnIndex = jtable.convertColumnIndexToView(jtable.getColumn(e.getKey()).getModelIndex());
jtable.getColumnModel().moveColumn(columnIndex, e.getValue());
}
This does not work, because moveColumn also moves other columns, so from docu:
void moveColumn(int columnIndex,
int newIndex)
Moves the column and its header at columnIndex to newIndex. The old column at columnIndex will now be found at newIndex. The column that used to be at newIndex is shifted left or right to make room.
Can i (dynamically) somehow put the columns in the exactly same order as in my map?