Reputation: 7380
I have a JTable with some columns. I have a HashMap of the column identifier mapped to the position in the view, for example:
TableHeader1 | TableHeader2 | TableHeader3
sth. sth. sth.
I know that:
TableHeader1 -> position 0
TableHeader2 -> position 1
TableHeader3 -> position 2
Now I want to reorder the columns. I know that there is a function called moveColumn(A, B) within the JTable class. This moves a column from A to B, and B is putted left or right. My problem is, I want to order the whole table in a specific way, how can I do this? If I use moveColumn, I cannot know where B has been moved, in 5 out of 10 cases it might be the right side and in the other cases the wrong side.
Hope you understand my problem :-)
Upvotes: 5
Views: 12519
Reputation: 164
This problem can be solved by using the built in moveColumn function as a bubble sort shortcut. The hashmap holds your weights in that case. Keep in mind that getColumnModel().getColumn(j).getModelIndex() can be seen as the initial column index / ID that will not change. See it as a column title.
HashMap<int, int> mapOrder = new HashMap<int, int>();
for (int i = 0; i < jt_table.getColumnCount(); i++) {
for (int j = 0; j < jt_table.getColumnCount() - 1; j++) {
int col1 = mapOrder.get(jt_table.getColumnModel().getColumn(j).getModelIndex());
int col2 = mapOrder.get(jt_table.getColumnModel().getColumn(j + 1).getModelIndex());
if (col1 > col2) {
jt_table.moveColumn(jt_table.getColumnModel().getColumn(j).getModelIndex(), jt_table.getColumnModel().getColumn(j + 1).getModelIndex());
}
}
}
Upvotes: 2
Reputation: 4443
Based on @Guillaume answer I found a way to do that without the need to remove all columns and add them again.
public static void setColumnOrder(int[] indices, JTable table, TableColumnModel columnModel) {
for (int i = 0; i < indices.length; i++) {
columnModel.moveColumn(i, table.convertColumnIndexToView(indices[i]));
}
}
This works better for me because with (SwingX) JXTable, the order of the invisible columns is not modified.
Upvotes: 1
Reputation: 324207
If you want to reorder by column name then you can check out the Table Column Reordering suggestion.
Upvotes: 0
Reputation: 18455
OK how about this. Might be a bit left field.
Extend TableColumn
and give your new class a position
property. Have it implement Comparable
and use the position
to compare columns.
Next, extend DefaultTableColumnModel
and store TableColumn
s in an ordered list.
Your JTable should now display columns according to their position
. Untested but it sounds interesting so I might give it a go later.
Upvotes: 1
Reputation: 14671
You can change the columns order by removing all of them and adding them in the right order:
public static void setColumnOrder(int[] indices, TableColumnModel columnModel) {
TableColumn column[] = new TableColumn[indices.length];
for (int i = 0; i < column.length; i++) {
column[i] = columnModel.getColumn(indices[i]);
}
while (columnModel.getColumnCount() > 0) {
columnModel.removeColumn(columnModel.getColumn(0));
}
for (int i = 0; i < column.length; i++) {
columnModel.addColumn(column[i]);
}
}
Upvotes: 8