views:

246

answers:

2

I have an empty JTable, absolutely nothing in it. I need to dynamically generate its table columns in a certain way. A simplified version for the code I have for my attempt:

@Action
public void AddCol() {
    for (int i = 0; i < 10; i++) {
        TableColumn c = new TableColumn(i);
        c.setHeaderValue(getColNam(i));
        table.getColumnModel().addColumn(c);
    }
}

But I'm getting an

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

What am I doing wrong?

Here's the complete stacktrace if it helps:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.elementAt(Vector.java:427)
        at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
        at engine.Processor$UpdateTable.run(Processor.java:131)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
+1  A: 

We don't have the complete StackTrace, so I can't tell for sure, but I guess that the exception is thrown at getColNam(i) where you could be referring to some empty Collection.

Try to replace it by

c.setHeaderValue("Test");

to find out.


If this is not the problem, you could try the TableColumn-Constructor without parameter:

TableColumn c = new TableColumn();
Peter Lang
getColName is just a mock method to show what I'm doing in the code..The problem with the no param constructor is, If I create the column without the Model Index, the table generated thinks it has only one column, so when I populate a row with setValueAt, it populates the whole row with the value at all the columns.
jonasespelita
+4  A: 

I think you need to add the column to your table's data model as well as its column model. The column model is updated when the data model changes so changing the data model should be sufficient. Here is an example:

public class TableColumnAdd {
    private static DefaultTableModel tableModel;
    private static int columnNumber = 1;

    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                tableModel = new DefaultTableModel(new Object[] { "Initial Column" }, 5);
                JTable table = new JTable(tableModel);
                JFrame frame = new JFrame("Table Column Add");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(100, 100, 600, 300);
                frame.add(new JScrollPane(table));
                frame.setVisible(true);
            }
        });

        for (;;) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    tableModel.addColumn("Column #" + columnNumber++);
                }
            });
            Thread.sleep(2000);
        }
    }
}
Russ Hayward