Search code examples
jtablerefreshevent-dispatch-threadmouselistenerinvokelater

Exception in thread "AWT-EventQueue-0" despite of using invokeLater


I'm trying to update a JTable (here: prosumerTable) programmatically, using SwingUtilities.invokeLater to start it out of the EventDispatchThread-

to be exact i have to replace the content completely, hence i'm just removing all rows and adding new ones - this works fine:

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();
        }

But if i add a MouseListener

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();
            }

        }
    });

to react on click Events i get an Exception.

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)

namely here, where i try to access the tableModel:

Prosumer p = (Prosumer) model.getValueAt(row, model.getColumnCount() - 1);

I assumed using the EventDispatchThread to update SwingComponents would be sufficient method to avoid exceptions of this type - or am i barking up the wrong tree here?


Solution

  • In addMouseListener,

    int row = target.getSelectedRow();
    

    Above statement will return -1 if there is no row selected.

    So you are getting ArrayIndexOutOfBoundsException.

    So before calling the model.getValueAt(row, model.getColumnCount() - 1); statement try to check whether there is/are rows selected or not.

    int row = target.getSelectedRow();
    // int column = target.getSelectedColumn();
    if(row != -1) {
       Prosumer p = (Prosumer) model.getValueAt(row, model.getColumnCount() - 1);
       CommandInspectNode c1 = new CommandInspectNode(p, false);
       c1.execute();
    }