calling invokeAndWait from the EDT

后端 未结 4 2003
忘了有多久
忘了有多久 2020-12-16 22:34

I have a problem following from my previous problem. I also have the code SwingUtillities.invokeAndWait somewhere else in the code base, but when I remove this

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 23:27

    You can check before if your current calling thread is already the event dispatcher:

    private void syncExec(final Runnable r) {
        try {
            if (EventQueue.isDispatchThread()) r.run();
            else EventQueue.invokeAndWait(r);
        } catch (final Exception e) {
            Throws.throwRuntime(e);
        }
    }
    

    Note that SwingUtilities.invokeAndWait(Runnable) simply delegates to the EventQueue.

提交回复
热议问题