我正在尝试以编程方式更新 JTable(此处:prosumerTable),使用 SwingUtilities.invokeLater 从 EventDispatchThread 中启动它-
确切地说,我必须完全替换内容,因此我只是删除所有行并添加新行 - 这很好用:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
controlledProsumers = v.getControlledProsumers();
if (model.getRowCount() > 0) {
for (int i = model.getRowCount() - 1; i > -1; i--) {
model.removeRow(i);
}
}
Iterator<Prosumer> it = controlledProsumers.iterator();
while (it.hasNext()) {
Prosumer p = it.next();
model.addRow(new Object[] { p.getProsumerName(), p.getProsumerType(), p.getChangeRate(), p.getMinimalOutput(), p.getMaximalOutput(),
p.getRealOutput(), p.getPredictedOutput(), p.getCredibility(), p.getReliability(), p.getPrice(), new MiniOutputPanel(p), p });
}
prosumerTable.packAll();
}
但是如果我添加一个 MouseListener
鼠标监听器:
prosumerTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTable target = (JTable) e.getSource();
int row = target.getSelectedRow();
// int column = target.getSelectedColumn();
Prosumer p = (Prosumer) model.getValueAt(row, model.getColumnCount() - 1);
CommandInspectNode c1 = new CommandInspectNode(p, false);
c1.execute();
}
}
});
对点击事件做出反应,我得到一个异常。
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 gui.prosumerViews.subpanels.controlledProsumersAvPP.ControlledProsumersAVPPPanel$1.mouseClicked(ControlledProsumersAVPPPanel.java:85)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(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)
即在这里,我尝试访问tableModel:
Prosumer p = (Prosumer) model.getValueAt(row, model.getColumnCount() - 1);
我假设使用 EventDispatchThread 来更新 SwingComponents 足以避免这种类型的异常 - 或者我在这里吠叫错误的树?