gpt4 book ai didi

java - ArrayIndexOutOfBoundsException : -1 on JTable creation/tablechanged

转载 作者:行者123 更新时间:2023-12-01 16:39:09 30 4
gpt4 key购买 nike

我已经阅读了所有看起来似乎可以解决我的问题的线程,并且我也阅读了这里的所有答案,但我仍然束手无策。我不确定为什么抛出异常,尽管我有一种感觉这可能与 threading 有关。 。如果是这样,请告诉我在哪里包含 new Runnable()以及是否 invokeLater()invokeAndWait() ,因为我已经尝试过了,没有效果。

请耐心等待,我将向您提供导致异常的代码+堆栈跟踪(如下)。

编辑:我包含了一些 syso位于 AnnoTable tableChanged 之前的部分被调用并且它们不会出现在控制台中,因此我认为问题甚至必须在应用程序到达该点之前发生,即从 AAView 调用它时或者当数据和表模型被实例化时......

编辑 II:问题是被覆盖的 tableChanged方法。这显然会引发异常。我已经删除了tableChanged()也可以调用(这不会有什么区别)。现在我遇到了另一个问题:了解基础数据 ( AnnoData ) 的更改如何自动更新表。虽然这可能是为了另一个查询(在扩展的 Google 搜索之后),但请随时在此线程中发布有用的评论,因为我将继续阅读它...非常感谢所有有用的评论和提示!

编辑 III:* 我已经解决了问题。我需要实例化 AnnoData 中的另一个对象,将其传递给 AnnoTableModel 的新实例,将此实例设置为我的表,然后 fireTableDataChanged() .

编辑 IV: 好的,所以 fireTableDataChanged() (如编辑 III 中使用的)毕竟是不必要的。我仍然想使用它而不是一直创建新对象。我想我应该问一个新问题...谢谢!

AAView中的此方法应该创建一个扩展 JTable 的对象,将其放入 JScrollPane等等(后者确实有效)。

private JPanel createAnnoTablePanel() {
annoTablePanel = new JPanel();
annoTable = new AnnoTable(aameth);
setAnnoTable(annoTable);
JScrollPane scrollPane = new JScrollPane(getAnnoTable());
annoTablePanel.add(scrollPane);
return annoTablePanel;
}

这是类(class) AnnoTable ( aameth 是一个实例对象,包含访问数据模型的业务逻辑,工作正常)。

public class AnnoTable extends JTable implements TableModelListener
{

public AnnoTable(AAMethods aameth)
{

int tokenCount = aameth.getTokenCount();

AnnoData annoData = new AnnoData(aameth); // cf. below, AnnoData is a Vector(Vector<Object>,String[])

TableModel tableModel = new AnnoTableModel(annoData.getAnnoData(),
// AnnoTableModel extends AbstractTableModel(Vector, String[])
annoData.getColTitles());
setModel(tableModel);
getModel().addTableModelListener(this);
TableModelEvent tme = new TableModelEvent(tableModel);
this.tableChanged(tme);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setCellSelectionEnabled(true);
getColumnModel().getSelectionModel().addListSelectionListener(new AnnoTableSelectionListener(this));
setPreferredScrollableViewportSize(this.getPreferredSize());

}

public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
AbstractTableModel model = (AbstractTableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column); // This is where the exception is thrown!
}
}

如果您需要 AnnoTableModel() 的源代码(这是 AbstractTableModel 的相当通用的扩展)或 AnnoData (它构造一个 Vector ,其中包含三个 Vector<Object> 和一个 String[] 作为列标题),请告诉我。

这是堆栈跟踪。

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at package.AnnoTable.tableChanged(AnnoTable.java:52)
at javax.swing.JTable.setModel(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at package.AnnoTable.<init>(AnnoTable.java:25)
at package.AAView.createAnnoTablePanel(AAView.java:464)
at package.AAView.createNorthPanel(AAView.java:455)
at package.AAView.displayAndAnnotate(AAView.java:444)
at package.AAView.loadProject(AAView.java:333)
at package.AAView.actionPerformed(AAView.java:286)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Actions.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
at javax.swing.JMenuBar.processKeyBinding(Unknown Source)
at javax.swing.KeyboardManager.fireBinding(Unknown Source)
at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(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)

我可以看到EDT在那里,据我所知,这可能确实是线程问题。但是,我不知道如何找出应该在哪里开始新的 Thread (或调用 new Runnable()

顺便说一句,当我更改 AnnoTable 时,异常才开始出现延长JTable而不是JPanel 。本来我有AnnoTable不仅构建表格,还将其包装在滚动 Pane 中并将其添加到新的 JPanel 中。 。但因为我想fireTableDataChanged来自只知道 AAView 的类(class)(它还具有 setAnnoTable() 方法)我想以正确的方式进行操作,而之前它工作得很好。墨菲定律?

最佳答案

rowIndex 为 -1 (==TableModelEvent.HEADER_ROW) 表示模型的结构已完全更改。此类事件由 setModel 上的 JTable 在内部触发。阅读 TableModelEvent 的 api 文档,以充分了解监听器的 tableChanged 中期望哪些类型/值。

顺便说一句,@AKJ 是对的 - 无需在表代码中触发任何 TableModelEvents。让模型适本地触发事件

关于java - ArrayIndexOutOfBoundsException : -1 on JTable creation/tablechanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7876090/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com