Question

I am writing some UI persistence methods for an application. I'm trying to expand to a JXTable with a fixed set of N columns in the table model.

How can I get and set the following information for column #k in the JXTable, where k is the column number in table model order? It's not as easy as it sounds, or at least I'm missing some tricky bit of information.

  • column width
  • column visibility
  • column order

TableColumnModelExt.getColumns() includes this crazy note, which I'm having trouble parsing:

java.util.List<javax.swing.table.TableColumn> getColumns(boolean includeHidden)

Returns a List of contained TableColumns. Includes or excludes invisible columns, depending on whether the includeHidden is true or false, respectively. If false, an Iterator over the List is equivalent to the Enumeration returned by getColumns().

NOTE: the order of columns in the List depends on whether or not the invisible columns are included, in the former case it's the insertion order in the latter it's the current order of the visible columns.

Also, I know how to use JTable.convertColumnIndexToView(), but it returns -1 for hidden columns, and leaves no information about which order the hidden column would go if it were made visible again. JXTable knows this information because you can restore column visibility, and it puts the column where it was before it was hidden.

Was it helpful?

Solution

If I understand you correctly, you are after the view column index a hidden column would have if made visible again. That's not directly supported.

For saving/restoring purposes, there's a class XProperties (not officially supported, but working smoothly) doing so in the context of AppFramework which might give you an idea of how to do it.

Edit

To get a TableColumn by modelIndex, get all columns, loop and compare the modelIndex. Some pseudo-code in a pseudo method getColumnFor(modelIndex):

List allColumns = table.getColumns(true);
forEach {
   if (column.getModelIndex() == modelIndex) {
        return column;
   }
return null;

OTHER TIPS

Consider using the Swing Application Framework (JSR-296)

Although this project is now dead AFAIK, I think it's a better starting point than implementing this feature from scratch. (It's Open Source.)

If you're building your application on top of the NetBeans Platform (which I highly recommend), then have a look at my blog on how to use these two frameworks together:

http://puces-blog.blogspot.com/2009/04/netbeans-platform-meets-swing.html

Note: There is also the Better Swing Application Framework

The Better Swing Application Framework is a fork of the original Swing Application Framework (appframework) reference implementation of JSR 296. Since August 2009, the original Swing Application Framework project has been on hold, and therefore this fork was created to carry on the work until the original project resumes. 

I don't know much about this, but it might be even a better starting point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top