2

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
AvrDragon
  • 7,139
  • 4
  • 27
  • 42

2 Answers2

3

The underlying problem is of course that Vector, which is used in the DefaultTableColumnModel to store the columns and their order has no decent move support. The move is achieved as a remove and add, which triggers the "move left or right" behavior.

You can however use this knowledge to simply move the columns in the correct order (from lowest index to highest index).

Starting from

|id | name |age |

to

|name | age | id |

can be achieved by first moving name to index 0. This will result in a

|name|id|age

vector. If you then move age to index 1, it will do a remove first

|name|id

followed by an insert at index 1

|name|age|id

By making sure the element is always moved to the left side of its original position, you know how the shifting will behave.

Of course this depends on an implementation detail, but write a quick unit test to support your assumptions and use those implementation details (or write your own TableColumnModel ;-) )

Robin
  • 36,233
  • 5
  • 47
  • 99
0

EDIT

No, i don't need to sort rows, i need to put the colums in order

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • No, i don't need to sort rows, i need to put the colums in order – AvrDragon Sep 25 '12 at 13:25
  • exactly, so the columns (id, name, age) -> (age, name, id) a – AvrDragon Sep 25 '12 at 13:29
  • see my edit, inc good way (my view) to helt arrays of TableColumns in the Stack, sure you can use any alternative :-) – mKorbel Sep 25 '12 at 13:33
  • @AvrDragon: You can have `Value` implement `Comparable` or supply a `Comparator`, but you'll still need a `SortedMap`. – trashgod Sep 25 '12 at 13:38
  • @trashgod no, i don't need a SortedMap. The sorting order is stored as Values. The order of how is sorted order stored itself in insigninicant:3 – AvrDragon Sep 25 '12 at 13:41
  • Perhaps I am misreading [this](http://docs.oracle.com/javase/7/docs/api/java/util/Map.html), "Some map implementations, like the `TreeMap` class, make specific guarantees as to their order; others, like the `HashMap` class, do not." – trashgod Sep 25 '12 at 13:56
  • @trashgod the entries in his map are indeed unordered, but the order is stored in the map. So he can loop over all entries, and determine for each column (=key) the corresponding index (=value). So he doesn't need an ordered map. He will have to go through the whole map before finding which column must be at index 0 however (same for other indices) – Robin Sep 25 '12 at 17:42
  • Ah, an in-place [insertion sort](http://en.wikipedia.org/wiki/Insertion_sort), of sorts. :-) Thanks! – trashgod Sep 25 '12 at 19:13