Java摆动多个JTables一个ColumnModel

时间:2013-10-22 15:11:56

标签: java swing jtable jtableheader

我有一个类,我创建一个没有数据且只有标题

的JTable
public class Test
{
    private static int COLUMN_WIDTH[] = {75, 150, 75};
    public static final String[] COLUMN_NAMES = {"One", "Two", "Three"};

    public static void main (String [] args)
    {
        JFrame frame = new JFrame("Test");
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

        // making column model and header
        JTable table = new JTable(null, COLUMN_NAMES);
        JTableHeader header = table.getTableHeader();
        TableColumnModel tableModel = header.getColumnModel();
        for(int i = 0; i < tableModel.getColumnCount(); i++)
        {
            tableModel.getColumn(i).setMinWidth(COLUMN_WIDTH[i]);
            tableModel.getColumn(i).setPreferredWidth(COLUMN_WIDTH[i]);
        }
        panel.add(header);

        // making children
        Children child = new Children(tableModel);
        child.add(panel);

        child = new Children(tableModel);
        child.add(panel);

        child = new Children(tableModel);
        child.add(panel);

        // adding all
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

此外,我的孩子们不断更改行数和值

public class Children
{
    private JTable table;

    public Children(TableColumnModel model)
    {
        table = new JTable(new Object[][] {{111, "One", 222}, {444, "Two", 333}, {555, "Three", 777}}, Test.COLUMN_NAMES);
        table.setColumnModel(model);
    }

    public void add(JPanel panel)
    {
        panel.add(table);
    }
}

我希望能够调整它们的大小,或者能够按照我想要的方式移动列。
但是列调整大小根本不起作用 每当我想移动专栏时,我都会得到这个

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JTable$1.getRowCount(JTable.java:666)
    at javax.swing.JTable.getRowCount(JTable.java:2627)
    at javax.swing.JTable.columnSelectionChanged(JTable.java:4614)
    at javax.swing.table.DefaultTableColumnModel.fireColumnSelectionChanged(DefaultTableColumnModel.java:588)
    at javax.swing.table.DefaultTableColumnModel.valueChanged(DefaultTableColumnModel.java:693)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
    at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:660)
    at javax.swing.table.DefaultTableColumnModel.moveColumn(DefaultTableColumnModel.java:180)
    at javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseDragged(BasicTableHeaderUI.java:226)
    at java.awt.AWTEventMulticaster.mouseDragged(AWTEventMulticaster.java:302)
    at java.awt.Component.processMouseMotionEvent(Component.java:6345)
    at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3293)
    at java.awt.Component.processEvent(Component.java:6066)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4660)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4253)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:647)
    at java.awt.EventQueue$3.run(EventQueue.java:645)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
    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)

点击任意列即可显示

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JTable$1.getRowCount(JTable.java:666)
    at javax.swing.JTable.getRowCount(JTable.java:2627)
    at javax.swing.JTable.columnSelectionChanged(JTable.java:4614)
    at javax.swing.table.DefaultTableColumnModel.fireColumnSelectionChanged(DefaultTableColumnModel.java:588)
    at javax.swing.table.DefaultTableColumnModel.valueChanged(DefaultTableColumnModel.java:693)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
    at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:442)
    at javax.swing.JTable.changeSelectionModel(JTable.java:2348)
    at javax.swing.JTable.changeSelection(JTable.java:2415)
    at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1085)
    at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1008)
    at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
    at java.awt.Component.processMouseEvent(Component.java:6294)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6062)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4660)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4233)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:647)
    at java.awt.EventQueue$3.run(EventQueue.java:645)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
    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 个答案:

答案 0 :(得分:3)

您已JTable创建了null rowData new JTable(null, COLUMN_NAMES)

因此,只要NullPointerException尝试访问JTable,您就会看到rowData毫无疑问。

如果您不想使用空表,请使用空数组而不是null数组。