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.
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.
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;