当前位置: 首页 > 知识库问答 >
问题:

渲染时的JTable撕裂

夏侯旻
2023-03-14

我已经建立了一个JTable,并使其包含18列。最初,当它是13列时,它非常适合JTable,并且没有撕裂。然而,一旦我使它包含18个表,并将自动调整大小设置为关闭,当我使用水平滚动条尝试滚动其他列时,JTable的撕裂开始发生,如图所示:

Netbeans还开始向我发送大量错误消息,如:

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

共有2个答案

司徒宏远
2023-03-14

答案是这样的:

java线程“AWT-EventQueue-0”中出现异常。lang.ArrayIndexOutofBounds异常:14 at

这意味着它知道它应该有更多的列,并且它想绘制下一列,但无法绘制,因为基础表模型没有那么多列。

为表指定18列意味着您还需要更新关联的表模型。两者都需要同步才能使表正常工作。

齐献
2023-03-14

我认为您已经声明了比数据数组中更多的列。

此代码示例准确地再现了您的问题:

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

现在,我建议使用另一种数据结构而不是数组,因为它不是非常灵活,而且它们往往很难维护。一个简单的解决方案是使用一些“模型”类并创建自己的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模式。

 类似资料:
  • 我试图使我的头的背景具有渐变。 这可以很好地使用代码: 然而,由于某种原因,我看不到我的边框和文本,似乎文本也应用了渐变? 我希望文本为黑色,各栏之间有彩色边框。有什么想法吗?

  • 问题内容: 我正在跟踪发现的一些代码,(是的,我知道它是如何工作的)它来自这里:代码链接 我想做的是如果单元格值设置为“黄色”,则设置单元格前景色 这是我的代码: 和单元格渲染器 问题是,如果我将任何单元格值设置为“黄色”,它就不会改变 提前致谢! 问题答案: 您的渲染器曾经使用过吗?您将其设置为包含String的单元格的默认渲染器,但是是否重载了模型的方法,以便它知道某些单元格包含String

  • 我有一个带有对象数组的组件,其中我正在根据字符串进行过滤。问题是当我尝试将此过滤器的返回设置为本地状态时,它会抛出错误,我不太理解原因。 所以,因为我希望这个数组处于我的状态,所以我决定这样做: 插入这一行后发生的事情是这样的: 它开始多次渲染。我假设,每次状态改变时,它都会重新渲染组件(如果我错了,请纠正我)。不过,我不知道它为什么要多次这样做。 因此,我想过使用 useEffect 来实现此处

  • 图片

  • 在 Hexo 中,有两个方法可用于渲染文件或字符串,分别是非同步的 hexo.render.render 和同步的 hexo.render.renderSync,这两个方法的使用方式十分类似,因此以下仅以非同步的 hexo.render.render 为例。 渲染字符串 在渲染字符串时,您必须指定 engine,如此一来 Hexo 才知道该使用哪个渲染引擎来渲染。 hexo.render.rend

  • 问题内容: 假设我有以下JTable,只要按下一个按钮,就会显示该JTable: 我想呈现 最初 对JCheckBox 为真的单元格,以及所有 最初为 false的所有单元格不显示任何内容(无JCheckBox)。用户可以选中或取消选中最初为真的单元格中的JCheckBoxes,这会对我创建的图表有所帮助。 现在,我的单元格渲染器在所有单元格中显示JCheckBoxes,包括最初为false的单元