sarah.ferguson sarah.ferguson - 1 year ago 122
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

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

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

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

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

The error is:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 1
at java.util.Vector.elementAt(
at javax.swing.table.DefaultTableColumnModel.getColumn(
at org.jdesktop.swingx.JXTable.getColumn(
at org.jdesktop.swingx.JXTable.getColumnExt(
at demo.table.AdvancedTableDemo.<init>(
at demo.table.AdvancedTableDemo$
at java.awt.event.InvocationEvent.dispatch(
at java.awt.EventQueue.dispatchEventImpl(
at java.awt.EventQueue.access$500(
at java.awt.EventQueue$
at java.awt.EventQueue$
at Method)
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(

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 Source

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--)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download