gpt4 book ai didi

Java swing多个JTables一个ColumnModel

转载 作者:行者123 更新时间:2023-12-01 23:29:42 25 4
gpt4 key购买 nike

我有一个类,我在其中创建一个没有数据且只有标题的 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);
}
}

此外,我的 child 的行数和值不断变化

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)

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

感谢您的建议。

最佳答案

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

因此,每当 JTable 尝试访问 rowData 时,您都会看到 NullPointerException,这并不奇怪。

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

关于Java swing多个JTables一个ColumnModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19521742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com