Java 摆动多个 JTables 一个 ColumnModel



我有一个类,我创建了一个没有数据的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)

关于如何解决这个问题的任何建议?如果我的方法错误,有人可以推荐一种如何快速有效地不断更改、添加和删除表中行的方法吗?

感谢您的任何建议。

您已经创建了一个带有 null rowDataJTable by new JTable(null, COLUMN_NAMES)

因此,每当JTable尝试访问rowData时,您都会看到NullPointerException也就不足为奇了。

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

相关内容

  • 没有找到相关文章

最新更新