动态改变 JTable 中的列数
我有一个 JTable
和一个扩展 AbstractTableModel
的 TableModel
。我想动态设置表中的列数。我通过向名为 column_count
的 TableModel
添加一个属性并让 getColumnCount
返回 column_count
来实现此目的。我还添加了一个方法 setColumnCount
,用于设置 column_count
并调用 fireTableStructureChanged
。不幸的是,当我运行该程序时,我不断收到 ArrayIndexOutOfBounds 异常。谁能告诉我我做错了什么,或者提出更好的解决方案?
这是堆栈跟踪:线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException: 4 >= 4 在 java.util.Vector.elementAt(来源未知) 在 javax.swing.table.DefaultTableColumnModel.getColumn(来源未知) 在 javax.swing.plaf.basic.BasicTableUI.paintGrid(来源未知) 在 javax.swing.plaf.basic.BasicTableUI.paint(来源未知) 在 javax.swing.plaf.ComponentUI.update(来源未知) 在 javax.swing.JComponent.paintComponent(来源未知) 在 javax.swing.JComponent.paint(来源未知) 在 javax.swing.JComponent.paintToOffscreen(来源未知) 在 javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(来源未知) 在 javax.swing.RepaintManager$PaintManager.paint(来源未知) 在 javax.swing.RepaintManager.paint(来源未知) 在 javax.swing.JComponent._paintImmediately(来源未知) 在 javax.swing.JComponent.paintImmediately(来源未知) 在 javax.swing.RepaintManager.paintDirtyRegions(来源未知) 在 javax.swing.RepaintManager.paintDirtyRegions(来源未知) 在 javax.swing.RepaintManager.seqPaintDirtyRegions(来源未知) 在 javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(来源未知) 在 java.awt.event.InitationEvent.dispatch(来源未知) 在 java.awt.EventQueue.dispatchEvent(来源未知) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(来源未知) 在 java.awt.EventDispatchThread.pumpEventsForFilter(来源未知) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(来源未知) 在 java.awt.EventDispatchThread.pumpEvents(来源未知) 在 java.awt.EventDispatchThread.pumpEvents(来源未知) 在 java.awt.EventDispatchThread.run(来源未知)
I have a JTable
and a TableModel
that extends AbstractTableModel
. I would like to dynamically set the number of columns in the table. I implemented this by adding an attribute to my TableModel
named, column_count
, and having getColumnCount
return the column_count
. I also added a method, setColumnCount
, that sets column_count
and calls fireTableStructureChanged
. Unfortunately, when I ran the program, I kept getting ArrayIndexOutOfBounds
exceptions. Can anyone tell me what I did wrong, or suggest a better solution?
Here's a stack trace:Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintGrid(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为异常是由如下原因引起的:
原因可能是因为 DefaultTableColumnModel 的底层代码不知道新列,并且其 Vector 尚未正确更改。
要解决此问题,您可能应该编写自己的自定义 TableColumnModel,它可以正确处理尺寸的变化。
I think the exception is caused as follows:
The reason is probably because the underlying code of DefaultTableColumnModel does not know about the new column, and its Vector has not properly been altered.
To fix this you should probably write your own custom TableColumnModel which can deal with changing dimensions properly.
您的
getValueAt()
方法从哪里获取数据?如果它是 ArrayList 并且您增加列大小,则表将尝试从列表中获取该列并引发异常。如果这不是问题,请使用 DefaultTableModel 和 DefaultTableModel.addColumn() 添加列,或确保从事件调度线程对表模型进行任何更改。
Where does your
getValueAt()
method takes data? If it is an ArrayList and you increase column size, than the table will try to fetch that column from the list and throw an exception.If that is not the problem, either use DefaultTableModel and DefaultTableModel.addColumn() to add columns, or make sure that you make any changes to the table model from the Event Dispatch Thread.
使用更改后的
TableModel
调用JTable
的setModel(tm)
方法在我的案例中解决了类似的错误。
Calling to method
setModel(tm)
ofJTable
with tm the changedTableModel
solved a similar error in my case.