动态地将列添加到JTable [英] Adding Columns to JTable dynamically

查看:81
本文介绍了动态地将列添加到JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个空的JTable,绝对没有。我需要以某种方式动态生成其表列。我尝试的代码的简化版本:

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

但我得到了


线程中的异常AWT-EventQueue-0java.lang.ArrayIndexOutOfBoundsException:0> = 0

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

我做错了什么?

如果有帮助,这是完整的堆栈跟踪:

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

这篇关于动态地将列添加到JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆