来自e4 EventHandler的JavaFX对话的无效线程访问

时间:2017-06-23 13:46:20

标签: multithreading javafx eclipse-rcp efxclipse

我有以下事件处理程序:

public class AssignPAMHandler {
    private static final Logger logger = Logger.getLogger(AssignPAMHandler.class);
    @Execute
    public void execute() {
        System.err.println(Thread.currentThread());
        showDialog();
    }
    private void showDialog() {
        Display.getDefault().asyncExec(() -> showDialogSWT());
    }
    private void showDialogSWT() {
        System.err.println(Thread.currentThread());
        new JFXPanel(); // initializes JavaFX environment
        Platform.runLater(() -> showDialogFX());
    }
    private void showDialogFX() {
        System.err.println(Thread.currentThread());
        TextInputDialog dialog = new TextInputDialog("walter");
        dialog.setTitle("Text Input Dialog");
        dialog.setHeaderText("Look, a Text Input Dialog");
        dialog.setContentText("Please enter your name:");
        dialog.showAndWait().ifPresent(name -> System.out.println("Your name: " + name));
    }
}

当我通过菜单项执行此处理程序时,我得到以下输出和异常(尽管如此,对话框仍显示):

Thread[main,6,main]
Thread[main,6,main]
Thread[JavaFX Application Thread,6,main]
Exception in thread "JavaFX Application Thread" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4533)
    at org.eclipse.swt.SWT.error(SWT.java:4448)
    at org.eclipse.swt.SWT.error(SWT.java:4419)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:483)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:421)
    at org.eclipse.swt.widgets.Widget.getData(Widget.java:537)
    at org.eclipse.e4.ui.internal.workbench.swt.ShellActivationListener.handleEvent(ShellActivationListener.java:61)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1605)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1339)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1366)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1345)
    at org.eclipse.swt.widgets.Shell.filterProc(Shell.java:807)
    at org.eclipse.swt.widgets.Display.filterProc(Display.java:1621)
    at com.sun.glass.ui.gtk.GtkApplication.enterNestedEventLoopImpl(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication._enterNestedEventLoop(GtkApplication.java:211)
    at com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:511)
    at com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:583)
    at javafx.stage.Stage.showAndWait(Stage.java:474)
    at javafx.scene.control.HeavyweightDialog.showAndWait(HeavyweightDialog.java:162)
    at javafx.scene.control.Dialog.showAndWait(Dialog.java:341)
    at net.openchrom.chromatogram.xxd.identifier.supplier.chromident.ui.handler.AssignPAMHandler.showDialogFX(AssignPAMHandler.java:54)

执行线程似乎始终相同,并显示FX对话框。然而,抛出异常。

1 个答案:

答案 0 :(得分:0)

JFXPanel替换为FXCanvas以使其运行。