SwingUtilities.invokeLater を使用して EventDispatchThread から開始し、プログラムで JTable (ここでは prosumerTable) を更新しようとしています。
正確には、コンテンツを完全に置き換える必要があるため、すべての行を削除して新しい行を追加するだけです-これは正常に機能します:
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 を更新するだけで十分だと思いました。