1

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??

5
  • Try this: instead of baseTable.setModel(model);, do final BaseTable baseTable = new BaseTable(model);. Rearrange lines as needed. Still have that problem then?
    – TT.
    Nov 8, 2016 at 14:34
  • There's no constructor with a model as parameter, only a constructor of BaseTable with no parameter. See source code: github.com/thirdy/swingtools/blob/master/src/main/java/com/… Nov 8, 2016 at 14:49
  • The reason I suggested that, is that for JTable.setModel() documentation it states: "Sets the data model for this table to newModel and registers with it for listener notifications from the new data model.". Nothing about setting up the column model for you (as in the constructor of JTable( TableModel )).
    – TT.
    Nov 8, 2016 at 14:57
  • the point where I hide the column is not dealing with the model actually but with a TableColumnExt, how that relates to the model I have yet to figure out. But I see no way of doing what you said for now. Nov 8, 2016 at 15:16
  • I suggest you alter the BaseTable class, add a constructor that takes a TableModel to call super(dm); (and does the other stuff in the parameter-less constructor), and use that constructor. By contract, the JXTable and JTable constructor for TableModel should create the ColumnModel for you.
    – TT.
    Nov 8, 2016 at 15:40

1 Answer 1

1

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--)
1
  • Thank you, I feel so dumb but really could not figure this out! I'll need some sleep... Nov 8, 2016 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.