2

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.

Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • why do you think it's crazy? Careful: I have written the doc and nearly everything else around JXTable :-) – kleopatra Dec 02 '11 at 18:40
  • OK, good point, let me be objective about this. JXTable uses different terms than JTable. JXTable uses "insertion order" and "current order of the visible columns" whereas JTable uses indices of the "view" and "model". If "view order" = "current order of the visible columns" and "model order" = "insertion order", then it would be extremely helpful if JXTable documentation corresponded to these notions as expressed by JTable.convertColumnIndexToView. – Jason S Dec 02 '11 at 18:50
  • 1
    see your objective point - but insertion order might be different from model order if .. well the columns initially were _not_ inserted in model order :-) With supporting hidden columns, JXTable adds an additional "dimension": making a hidden colum visible again must support moving it nearest to its view position at the moment of having been hidden, indepent of moving visible columns around. So there is third coordinate system: current view (for currently visible), potential view (when all hidden are visible) and model. Contributions to formulate that fact better are happily accepted :-) – kleopatra Dec 02 '11 at 19:04

2 Answers2

3

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;
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Hmm. Well, ok, what's the easiest way to get a TableColumn out of JXTable (whether it's visible or not) given the model index? – Jason S Dec 02 '11 at 19:30
1

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.

Puce
  • 37,247
  • 13
  • 80
  • 152
  • Thanks, but my question really isn't about persistence; I already have a framework which works for me. My question is about getting/setting the information on the GUI component. – Jason S Dec 02 '11 at 18:59