简体   繁体   中英

Delete item from JList in a different thread than EDT

I am trying to delete an element from a JList while looping through a for loop. To find out which element I want to delete, I do a comparison of a tooltip I have defined, and delete only that element.

public static void removeElement(JList list, String idx){
    DefaultListModel model = (DefaultListModel)list.getModel();
    JLabel actual;
    for(int i = 0; i < model.getSize(); i++){
        actual = (JLabel) model.get(i);
         if(actual.getToolTipText().equals(idx)){
             model.remove(i);
             break;
         }
    }
}

This function is declared in the main class, and accesses swing elements, also declared as static. The function is then invoked by another thread:

public class threadExample extends Thread{
    //...
    @Override
    public void run() {
        Main.removeElement(OIS.listElement, graphicalID);
    }
    
}

When executing this exception comes up, BUT only sometimes.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    t java.base/java.util.Vector.elementAt(Vector.java:497)
    at java.desktop/javax.swing.DefaultListModel.getElementAt(DefaultListModel.java:90)
    at java.desktop/javax.swing.plaf.basic.BasicListUI.paintCell(BasicListUI.java:265)
    at java.desktop/javax.swing.plaf.basic.BasicListUI.paintImpl(BasicListUI.java:378)
    at java.desktop/javax.swing.plaf.basic.BasicListUI.paint(BasicListUI.java:301)
    at java.desktop/javax.swing.plaf.synth.SynthListUI.update(SynthListUI.java:78)
    at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:797)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)
    at java.desktop/javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:246)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
    at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5203)
    at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5013)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:865)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1890)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

From what I have read, I cannot make the removal within the for loop like I am doing, but I can't find any alternative. Can anyone help me?

EDIT

The problem is not with the removal process, but because I am performing the operation in a different thread than EDT.

Since it seems that you are calling removeElement in a different thread than EDT you should use:

SwingUtilities.invokeLater(() -> { removeElement(....); }

for asynchronous execution, or

SwingUtilities.invokeAndWait(() -> { removeElement(....); }

if you want to wait for the method to complete before going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM