2

を設定し、JTable18列を含めるようにしました。当初、13カラムのときは、にぴったりとはまり、JTable破れはありませんでした。ただし、18個のテーブルを含めるようにし、自動サイズ変更をオフに設定すると、水平スクロールバーを使用して他の列をスクロールしようとすると、次のJTable図に示すように、ティアリングが発生し始めました。

テーブル画像

Netbeansは、次のような多くのエラーメッセージも表示し始めました。

スレッド「AWT-EventQueue-0」の例外java.lang.ArrayIndexOutOfBoundsException:14
    javax.swing.JTable $ 1.getValueAt(JTable.java:689)で
    javax.swing.JTable.getValueAt(JTable.java:2720)で
    javax.swing.JTable.prepareRenderer(JTable.java:5718)で
    javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)で
    javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)で
    javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)で
    javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)で
    javax.swing.JComponent.paintComponent(JComponent.java:778)で
    javax.swing.JComponent.paint(JComponent.java:1054)で
    javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)で
    javax.swing.RepaintManager $ PaintManager.paintDoubleBuffered(RepaintManager.java:1482)で
    javax.swing.RepaintManager $ PaintManager.paint(RepaintManager.java:1413)で
    javax.swing.RepaintManager.paint(RepaintManager.java:1206)で
    javax.swing.JComponent.paintForceDoubleBuffered(JComponent.java:1090)で
    javax.swing.JViewport.blitDoubleBuffered(JViewport.java:1598)で
    javax.swing.JViewport.windowBlitPaint(JViewport.java:1559)で
    javax.swing.JViewport.setViewPosition(JViewport.java:1102)で
    javax.swing.plaf.basic.BasicScrollPaneUI $ Handler.hsbStateChanged(BasicScrollPaneUI.java:1087)で
    javax.swing.plaf.basic.BasicScrollPaneUI $ Handler.stateChanged(BasicScrollPaneUI.java:1038)で
    javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:364)で
    javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:302)で
    javax.swing.DefaultBoundedRangeModel.setValue(DefaultBoundedRangeModel.java:168)で
    javax.swing.JScrollBar.setValue(JScrollBar.java:463)で
    javax.swing.plaf.basic.BasicScrollBarUI $ TrackListener.setValueFrom(BasicScrollBarUI.java:1253)で
    javax.swing.plaf.basic.BasicScrollBarUI $ TrackListener.mouseDragged(BasicScrollBarUI.java:1195)で
    java.awt.Component.processMouseMotionEvent(Component.java:6553)で
    javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3339)で
    java.awt.Component.processEvent(Component.java:6274)で
    java.awt.Container.processEvent(Container.java:2229)で
    java.awt.Component.dispatchEventImpl(Component.java:4861)で
    java.awt.Container.dispatchEventImpl(Container.java:2287)で
    java.awt.Component.dispatchEvent(Component.java:4687)で
    java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)で
    java.awt.LightweightDispatcher.processMouseEvent(Container.java:4509)で
    java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)で
    java.awt.Container.dispatchEventImpl(Container.java:2273)で
    java.awt.Window.dispatchEventImpl(Window.java:2719)で
    java.awt.Component.dispatchEvent(Component.java:4687)で
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)で
    java.awt.EventQueue.access $ 000(EventQueue.java:102)で
    java.awt.EventQueue $ 3.run(EventQueue.java:662)で
    java.awt.EventQueue $ 3.run(EventQueue.java:660)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)で
    java.awt.EventQueue $ 4.run(EventQueue.java:676)で
    java.awt.EventQueue $ 4.run(EventQueue.java:674)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.awt.EventQueue.dispatchEvent(EventQueue.java:673)で
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)で
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)で
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)で
    java.awt.EventDispatchThread.run(EventDispatchThread.java:97)で
スレッド「AWT-EventQueue-0」の例外java.lang.ArrayIndexOutOfBoundsException:14
    javax.swing.JTable $ 1.getValueAt(JTable.java:689)で
    javax.swing.JTable.getValueAt(JTable.java:2720)で
    javax.swing.JTable.prepareRenderer(JTable.java:5718)で
    javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)で
    javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)で
    javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)で
    javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)で
    javax.swing.JComponent.paintComponent(JComponent.java:778)で
    javax.swing.JComponent.paint(JComponent.java:1054)で
    javax.swing.JComponent.paintChildren(JComponent.java:887)で
    javax.swing.JComponent.paint(JComponent.java:1063)で
    javax.swing.JViewport.paint(JViewport.java:731)で
    javax.swing.JComponent.paintChildren(JComponent.java:887)で
    javax.swing.JComponent.paint(JComponent.java:1063)で
    javax.swing.JComponent.paintChildren(JComponent.java:887)で
    javax.swing.JComponent.paint(JComponent.java:1063)で
    javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)で
    javax.swing.RepaintManager $ PaintManager.paintDoubleBuffered(RepaintManager.java:1482)で
    javax.swing.RepaintManager $ PaintManager.paint(RepaintManager.java:1413)で
    javax.swing.RepaintManager.paint(RepaintManager.java:1206)で
    javax.swing.JComponent._paintImmediately(JComponent.java:5169)で
    javax.swing.JComponent.paintImmediately(JComponent.java:4980)で
    javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)で
    javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)で
    javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)で
    javax.swing.RepaintManager.access $ 700(RepaintManager.java:59)で
    javax.swing.RepaintManager $ ProcessingRunnable.run(RepaintManager.java:1621)で
    java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)で
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)で
    java.awt.EventQueue.access $ 000(EventQueue.java:102)で
    java.awt.EventQueue $ 3.run(EventQueue.java:662)で
    java.awt.EventQueue $ 3.run(EventQueue.java:660)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.awt.EventQueue.dispatchEvent(EventQueue.java:671)で
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)で
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)で
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)で
    java.awt.EventDispatchThread.run(EventDispatchThread.java:97)で
スレッド「AWT-EventQueue-0」の例外java.lang.ArrayIndexOutOfBoundsException:14
    javax.swing.JTable $ 1.getValueAt(JTable.java:689)で
    javax.swing.JTable.getValueAt(JTable.java:2720)で
    javax.swing.JTable.prepareRenderer(JTable.java:5718)で
    javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)で
    javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)で
    javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)で
    javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)で
    javax.swing.JComponent.paintComponent(JComponent.java:778)で
    javax.swing.JComponent.paint(JComponent.java:1054)で
    javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)で
    javax.swing.RepaintManager $ PaintManager.paintDoubleBuffered(RepaintManager.java:1482)で
    javax.swing.RepaintManager $ PaintManager.paint(RepaintManager.java:1413)で
    javax.swing.RepaintManager.paint(RepaintManager.java:1206)で
    javax.swing.JComponent.paintForceDoubleBuffered(JComponent.java:1090)で
    javax.swing.JViewport.blitDoubleBuffered(JViewport.java:1598)で
    javax.swing.JViewport.windowBlitPaint(JViewport.java:1559)で
    javax.swing.JViewport.setViewPosition(JViewport.java:1102)で
    javax.swing.plaf.basic.BasicScrollPaneUI $ Handler.hsbStateChanged(BasicScrollPaneUI.java:1087)で
    javax.swing.plaf.basic.BasicScrollPaneUI $ Handler.stateChanged(BasicScrollPaneUI.java:1038)で
    javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:364)で
    javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:302)で
    javax.swing.DefaultBoundedRangeModel.setValue(DefaultBoundedRangeModel.java:168)で
    javax.swing.JScrollBar.setValue(JScrollBar.java:463)で
    javax.swing.plaf.basic.BasicScrollBarUI $ TrackListener.setValueFrom(BasicScrollBarUI.java:1253)で
    javax.swing.plaf.basic.BasicScrollBarUI $ TrackListener.mouseDragged(BasicScrollBarUI.java:1195)で
    java.awt.Component.processMouseMotionEvent(Component.java:6553)で
    javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3339)で
    java.awt.Component.processEvent(Component.java:6274)で
    java.awt.Container.processEvent(Container.java:2229)で
    java.awt.Component.dispatchEventImpl(Component.java:4861)で
    java.awt.Container.dispatchEventImpl(Container.java:2287)で
    java.awt.Component.dispatchEvent(Component.java:4687)で
    java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)で
    java.awt.LightweightDispatcher.processMouseEvent(Container.java:4509)で
    java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)で
    java.awt.Container.dispatchEventImpl(Container.java:2273)で
    java.awt.Window.dispatchEventImpl(Window.java:2719)で
    java.awt.Component.dispatchEvent(Component.java:4687)で
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)で
    java.awt.EventQueue.access $ 000(EventQueue.java:102)で
    java.awt.EventQueue $ 3.run(EventQueue.java:662)で
    java.awt.EventQueue $ 3.run(EventQueue.java:660)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)で
    java.awt.EventQueue $ 4.run(EventQueue.java:676)で
    java.awt.EventQueue $ 4.run(EventQueue.java:674)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.awt.EventQueue.dispatchEvent(EventQueue.java:673)で
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)で
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)で
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)で
    java.awt.EventDispatchThread.run(EventDispatchThread.java:97)で
スレッド「AWT-EventQueue-0」の例外java.lang.ArrayIndexOutOfBoundsException:14
    javax.swing.JTable $ 1.getValueAt(JTable.java:689)で
    javax.swing.JTable.getValueAt(JTable.java:2720)で
    javax.swing.JTable.prepareRenderer(JTable.java:5718)で
    javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)で
    javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)で
    javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)で
    javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)で
    javax.swing.JComponent.paintComponent(JComponent.java:778)で
    javax.swing.JComponent.paint(JComponent.java:1054)で
    javax.swing.JComponent.paintChildren(JComponent.java:887)で
    javax.swing.JComponent.paint(JComponent.java:1063)で
    javax.swing.JViewport.paint(JViewport.java:731)で
    javax.swing.JComponent.paintChildren(JComponent.java:887)で
    javax.swing.JComponent.paint(JComponent.java:1063)で
    javax.swing.JComponent.paintChildren(JComponent.java:887)で
    javax.swing.JComponent.paint(JComponent.java:1063)で
    javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)で
    javax.swing.RepaintManager $ PaintManager.paintDoubleBuffered(RepaintManager.java:1482)で
    javax.swing.RepaintManager $ PaintManager.paint(RepaintManager.java:1413)で
    javax.swing.RepaintManager.paint(RepaintManager.java:1206)で
    javax.swing.JComponent._paintImmediately(JComponent.java:5169)で
    javax.swing.JComponent.paintImmediately(JComponent.java:4980)で
    javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)で
    javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)で
    javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)で
    javax.swing.RepaintManager.access $ 700(RepaintManager.java:59)で
    javax.swing.RepaintManager $ ProcessingRunnable.run(RepaintManager.java:1621)で
    java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)で
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)で
    java.awt.EventQueue.access $ 000(EventQueue.java:102)で
    java.awt.EventQueue $ 3.run(EventQueue.java:662)で
    java.awt.EventQueue $ 3.run(EventQueue.java:660)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.awt.EventQueue.dispatchEvent(EventQueue.java:671)で
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)で
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)で
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)で
    java.awt.EventDispatchThread.run(EventDispatchThread.java:97)で
スレッド「AWT-EventQueue-0」の例外java.lang.ArrayIndexOutOfBoundsException:14
    javax.swing.JTable $ 1.getValueAt(JTable.java:689)で
    javax.swing.JTable.getValueAt(JTable.java:2720)で
    javax.swing.JTable.prepareRenderer(JTable.java:5718)で
    javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)で
    javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)で
    javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)で
    javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)で
    javax.swing.JComponent.paintComponent(JComponent.java:778)で
    javax.swing.JComponent.paint(JComponent.java:1054)で
    javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)で
    javax.swing.RepaintManager $ PaintManager.paintDoubleBuffered(RepaintManager.java:1482)で
    javax.swing.RepaintManager $ PaintManager.paint(RepaintManager.java:1413)で
    javax.swing.RepaintManager.paint(RepaintManager.java:1206)で
    javax.swing.JComponent.paintForceDoubleBuffered(JComponent.java:1090)で
    javax.swing.JViewport.blitDoubleBuffered(JViewport.java:1598)で
    javax.swing.JViewport.windowBlitPaint(JViewport.java:1559)で
    javax.swing.JViewport.setViewPosition(JViewport.java:1102)で
    javax.swing.plaf.basic.BasicScrollPaneUI $ Handler.hsbStateChanged(BasicScrollPaneUI.java:1087)で
    javax.swing.plaf.basic.BasicScrollPaneUI $ Handler.stateChanged(BasicScrollPaneUI.java:1038)で
    javax.swing.DefaultBoundedRangeModel.fireStateChanged(DefaultBoundedRangeModel.java:364)で
    javax.swing.DefaultBoundedRangeModel.setRangeProperties(DefaultBoundedRangeModel.java:302)で
    javax.swing.DefaultBoundedRangeModel.setValue(DefaultBoundedRangeModel.java:168)で
    javax.swing.JScrollBar.setValue(JScrollBar.java:463)で
    javax.swing.plaf.basic.BasicScrollBarUI $ TrackListener.setValueFrom(BasicScrollBarUI.java:1253)で
    javax.swing.plaf.basic.BasicScrollBarUI $ TrackListener.mouseDragged(BasicScrollBarUI.java:1195)で
    java.awt.Component.processMouseMotionEvent(Component.java:6553)で
    javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3339)で
    java.awt.Component.processEvent(Component.java:6274)で
    java.awt.Container.processEvent(Container.java:2229)で
    java.awt.Component.dispatchEventImpl(Component.java:4861)で
    java.awt.Container.dispatchEventImpl(Container.java:2287)で
    java.awt.Component.dispatchEvent(Component.java:4687)で
    java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)で
    java.awt.LightweightDispatcher.processMouseEvent(Container.java:4509)で
    java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)で
    java.awt.Container.dispatchEventImpl(Container.java:2273)で
    java.awt.Window.dispatchEventImpl(Window.java:2719)で
    java.awt.Component.dispatchEvent(Component.java:4687)で
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)で
    java.awt.EventQueue.access $ 000(EventQueue.java:102)で
    java.awt.EventQueue $ 3.run(EventQueue.java:662)で
    java.awt.EventQueue $ 3.run(EventQueue.java:660)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)で
    java.awt.EventQueue $ 4.run(EventQueue.java:676)で
    java.awt.EventQueue $ 4.run(EventQueue.java:674)で
    java.security.AccessController.doPrivileged(ネイティブメソッド)で
    java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)で
    java.awt.EventQueue.dispatchEvent(EventQueue.java:673)で
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)で
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)で
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)で
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)で
    java.awt.EventDispatchThread.run(EventDispatchThread.java:97)で

なんでそうなの?この問題の解決策はありますか?ありがとう!

ごめんなさい!テーブルを初期化するために使用されるコードは次のとおりです。お役に立てば幸いです。

jp.setLayout(new BorderLayout());
    JTable jt = new JTable(data, column);// data and columns are just 2D and 1D String arrays each
    JTableHeader th = jt.getTableHeader();
    th.setBackground(java.awt.Color.pink);
    th.setEnabled(true);
    jt.setTableHeader(th);
    jt.setEnabled(false);
    jp.add(jt, BorderLayout.CENTER);
    JScrollPane scrollPane = new JScrollPane(jt, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    scrollPane.setEnabled(false);
    jp.add(scrollPane, BorderLayout.CENTER);
4

2 に答える 2

5

データ配列にあるよりも多くの列を宣言したと思います。

このコードサンプルは、問題を正確に再現しています。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class TestTable {

    public JFrame f;
    private JTable table;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable().initUI();
            }
        });
    }

    protected void initUI() {
        String[][] data = new String[1][1];
        data[0] = new String[] { "hello" };
        String[] columns = new String[] { "Column-1", "Column-2" };
        table = new JTable(data, columns);
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.add(new JScrollPane(table));
        f.setVisible(true);
    }

}

もちろん、解決策は、データ内の列の数を宣言した列の数と一致させることです。また、データ配列の各「行」が正しい数の「列」で初期化されていることを確認する必要があります。

たとえば、次のようにすることができます。

String[][] data = new String[1][18];
data[0] = new String[15]; // <-- This will cause some problems

ここで、配列とは別のデータ構造を使用することをお勧めします。これは、柔軟性が低く、保守が困難になる傾向があるためです。簡単な解決策は、「Model」クラスを作成し、次のような独自のTableModelを作成することです。

import java.util.List;
import java.util.Random;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

public class TestTable {

    public JFrame f;
    private JTable table;

    public class ModelObject {
        private String column0; // Consider using something more explicit than column0,1,...
        private String column1;

        public String getColumn0() {
            return column0;
        }

        public void setColumn0(String column0) {
            this.column0 = column0;
        }

        public String getColumn1() {
            return column1;
        }

        public void setColumn1(String column1) {
            this.column1 = column1;
        }
    }

    private class TableModel extends AbstractTableModel {

        private List<ModelObject> data;

        protected TableModel(List<ModelObject> data) {
            super();
            this.data = data;
        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
            case 0:
                return "Header 0";
            case 1:
                return "Header 1";
            }
            System.err.println("Only two columns are known in this table model");
            return "???";
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            ModelObject o = data.get(rowIndex);
            switch (columnIndex) {
            case 0:
                return o.getColumn0();
            case 1:
                return o.getColumn1();
            }
            System.err.println("Only two columns are known in this table model");
            return null;
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTable().initUI();
            }
        });
    }

    protected void initUI() {
        Random r = new Random();
        Vector<ModelObject> data = new Vector<TestTable.ModelObject>();
        for (int i = 0; i < 10; i++) {
            ModelObject o = new ModelObject();
            o.setColumn0("Some stuff in column 0 " + i);
            o.setColumn1("Some other stuff in column 1 " + r.nextInt());
            data.add(o);
        }
        table = new JTable(new TableModel(data));
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.add(new JScrollPane(table));
        f.setVisible(true);
    }

}

ここではすべてが単一のクラスで記述されていますが、もちろん、MVCパターンに正しく従うためにそれらを分離する必要があります。

于 2012-05-24T07:52:00.853 に答える
1

答えは次の行にあります。

スレッド「AWT-EventQueue-0」の例外java.lang.ArrayIndexOutOfBoundsException:14 at

これは、より多くの列があるはずであり、次の列を描画したかったことを認識していることを意味しますが、基になるテーブルモデルにそれほど多くの列がなかったため、できませんでした。

テーブルに18列を指定すると、関連するテーブルモデルも更新する必要があります。テーブルが機能するには、両方が同期している必要があります。

于 2012-05-24T06:22:33.143 に答える