无效线程访问:AWT ActionListener 中的 SWT

Invalid Thread Access: SWT in AWT ActionListener

试图摆脱此 SWTException:无效的线程访问。从 JButton ActionListener 中生成。最终目的是让按钮打开浏览器 window,导航到 URL,然后 URL 返回打开对话框...

private static final Display display = Display.getDefault();

// Fired from JButton:
class ShowBrowserAction implements java.awt.event.ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

        // Place-holder UI Update...
        display.asyncExec(new Runnable() {
            public void run() {
              System.out.println("Async task run");
            }
        });

        // Blocking until UI element is done...
        while (!display.isDisposed()) {
           // Always gives a thread access error, but still calls async event:  
           if ( !display.readAndDispatch()  )
                display.sleep();
        }

    }   
}

想法?

您只能在 SWT UI 线程上调用 SWT 操作。不支持在其他任何地方调用它们。

如果您想等待 UI runnable 完成,请使用 syncExec 而不是 asyncExec

你说好像可以,但是不同平台会有差异。例如在 macOS 上它肯定会完全失败。