sarah.ferguson sarah.ferguson - 1 month ago 21
Java Question

Swingtools extension of JXTable and JTable, hiding columns return index out of bounds

I'm evaluating Swingtools JTable, an open source and more powerful version of JTable and JXTable. As the maintainer Ivan Portyankin explains it on his blog ipsoftware blog:


"Sometimes it may seem that standard Swing JTable or its close
companions like SwingX JXTable are too limited. You don’t have too
many funny ways to manipulate them – they are strict rectangular
grids, with every row taking fixed amount of screen space in pixels
and columns taken from one and only column model. Columns cannot be
spanned or split and always take a single cell in the grid."


Now I am investigating the ability of hiding columns, this can be done via its interface: swingtools table However I'd like to hide by default some columns and store the preference when the user manually re-enables them so to show always the last chosen preference.

Below I try to hide all columns as default but I incur in an
ArrayIndexOutOfBoundsException
error!!

public class AdvancedTableDemo extends JFrame
{
public AdvancedTableDemo()
{
super("Advanced Table Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 300);

final BaseTable baseTable = new BaseTable();
baseTable.setFilterHeaderEnabled(true);
add(new JScrollPane(baseTable));
BeanPropertyTableModel<TableBean> model = new BeanPropertyTableModel<TableBean>(TableBean.class);
model.setOrderedProperties(Arrays.asList("name", "surname", "date"));
model.setData(TableBean.generateList(100));
baseTable.setModel(model);

int j = baseTable.getColumnCount(true);
for (int i = 0; i < j; i++)
{
TableColumnExt tableColumnExt = baseTable.getColumnExt(i);
tableColumnExt.setVisible(false);
}
setVisible(true);
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new AdvancedTableDemo();
}
});
}
}


The error is:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 1
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
at org.jdesktop.swingx.JXTable.getColumn(JXTable.java:2265)
at org.jdesktop.swingx.JXTable.getColumnExt(JXTable.java:2415)
at demo.table.AdvancedTableDemo.<init>(AdvancedTableDemo.java:46)
at demo.table.AdvancedTableDemo$1.run(AdvancedTableDemo.java:62)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


The column count is 3, but getting anything greater than column 1 yields the error just seen! tried with 17 columns and anything greater than 10 returns exception.

Am I missing something here??

Answer

The column count is 3, but getting anything greater than column 1 yields the error

int j = baseTable.getColumnCount(true);
for (int i = 0; i < j; i++)

Yes, but after you hide the first column there are only 2. Then after you hide the next column there is only one (so you get the exception).

So the solution is to hide the columns from the end of the table, not the beginning:

int j = baseTable.getColumnCount(true) -1;
for (int i = j; i >= 0;  i--)