Java Tips Weblog

  • Blog Stats

    • 1,759,074 hits
  • Categories

  • Archives

Table Column Reordering

Posted by Rob Camick on January 9, 2009

By default table columns are created and displayed in the order in which the column data is created in the TableModel. This will satisfy your requirements the majority of the time as in most case you are in control of how the data in the TableModel is created. However, there may be some situations when you wish to change the default ordering of the table columns.

For example, maybe your application allows the user to control the ordering of columns and you therefore need to reorder the columns for each user every time the application is started. Or, maybe the columns are created dynamically and you have no control over the order in which the columns are created. An example of this would be how the columns are created using the Bean Table Model, presented in a previous entry.

Well, we can’t easily change the default ordering, but we can reorder the columns after they have been created. The TableColumnManager provides the moveColumn() method which allows you to reorder a TableColumn. A static convenience method has been added to the RXTable class to facilitate reordering of columns:

  • reorderColumns – reorder the columns of the table based on the column names found in the given array

For example, to specify the first two columns to be displayed in a given table you could do:

RXTable.reorderColumns(table, "Column Name 1", "Column Name 2");

Or, if you have many columns to reorder you would probably use:

Object[] columnNames = new Object[?];
columnNames[0] = "Column Name 1";
columnNames[1] = "Column Name 2";
RXTable.reorderColumns(table, columnNames);

Hopefully you find this approach easier to use than by invoking the moveColumn() method manually.

Thanks to Andre Uhres for providing the basic code of the reorderColumns method. The original code can be found in the comments section of the Bean Table Model entry.

Get The Code

See Also

Bean Table Model

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: