我已經設置了JTable
,並使其包含18列。最初,當它是13列,它很好地適合JTable
,並沒有撕裂。但是,一旦我使它包含18個表並將自動調整大小設置爲關閉,當我使用水平滾動條嘗試滾動其他列時,開始發生JTable
的撕裂,如此圖所示:JTable在呈現時撕裂
table image http://oi46.tinypic.com/2uzupg0.jpg
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);
你的代碼顯然有些問題。你看到的堆棧跟蹤可能是你問題的原因。發佈一些重現問題的代碼。 –
在一些代碼中添加! – Laughy