动态改变 JTable 中的列数

发布于 2024-10-06 14:26:16 字数 1901 浏览 5 评论 0原文

我有一个 JTable 和一个扩展 AbstractTableModelTableModel。我想动态设置表中的列数。我通过向名为 column_countTableModel 添加一个属性并让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

错爱 2024-10-13 14:26:16

我认为异常是由如下原因引起的:

  1. 您将内部column_count设置为+1
  2. 您导致事件被触发,这将导致表以可视方式更新
  3. 当JTable更新代码访问最后一列时,列模型中的内部Vector抛出异常。

原因可能是因为 DefaultTableColumnModel 的底层代码不知道新列,并且其 Vector 尚未正确更改。

要解决此问题,您可能应该编写自己的自定义 TableColumnModel,它可以正确处理尺寸的变化。

I think the exception is caused as follows:

  1. You set internal column_count to +1
  2. You cause events to be fired which will cause the table to be updated visually
  3. When the JTable update code accesses the last column the internal Vector in the column model throws the exception.

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.

携君以终年 2024-10-13 14:26:16

您的 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.

來不及說愛妳 2024-10-13 14:26:16

使用更改后的 TableModel 调用 JTablesetModel(tm) 方法
在我的案例中解决了类似的错误。

Calling to method setModel(tm) of JTable with tm the changed TableModel
solved a similar error in my case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文