开发者

Adding Columns to JTable dynamically

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)


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);
        }
    }
}


If you want to add a list of string column names in a quick way, use this.

DefaultTableModel tableModel = new DefaultTableModel();

for(String columnName : columnNames){
   tableModel.addColumn(columnName);
}

jTable.setModel(tableModel);


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜