0

データがなくヘッダーのみの 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)

この問題を解決する方法について何か提案はありますか? 私のアプローチが間違っている場合、テーブルから行をすばやく効率的に変更および追加および削除し続ける方法を誰かが推奨できますか?

アドバイスをありがとう。

4

1 に答える 1

3

でを作成しJTableましnull rowDatanew JTable(null, COLUMN_NAMES)

したがって、が にアクセスしようとするNullPointerExceptionたびに sが表示されるのは当然のことです。JTablerowData

空のテーブルを使用したくない場合は、配列の代わりに空の配列を使用してくださいnull

于 2013-10-22T15:33:51.570 に答える