线程:SWT事件

时间:2017-04-27 13:56:21

标签: java swt eclipse-rcp

我有一个SWT TreeViewer触发选择更改事件。 奇怪的是,这个事件是在不同的线程上处理的,这取决于哪个线程是最后一个线程(看起来像)。

如果我刚启动应用程序(Eclipse RCP),事件将在main主题上处理。

如果我之前打开过JavaFX视图,事件由JavaFX Application Thread 处理。

我假设所有SWT UI事件都由专用的SWT UI线程处理(就像JavaFX和Swing一样)?

Eclipse RCP应用程序使用一些JavaFX视图(e(fx)clipse

这是一个最小的工作示例(虽然不是Eclipse RCP,但是Maven)。它显示所有事件都在“JavaFX Application Thread”上处理。但我认为它是同一个线程,它只是“重命名”?

输出如下:

Hello from 1, main
Hello from 1, JavaFX Application Thread
Hello from 1, JavaFX Application Thread

这里是班级:

public class Main {

public static void main(String[] args) {

    System.err.println("Hello from " + Thread.currentThread().getId() + ", " + Thread.currentThread().getName());
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final RowLayout layout = new RowLayout();
    shell.setLayout(layout);
    final org.eclipse.swt.widgets.Button swtButton = new org.eclipse.swt.widgets.Button(shell, SWT.PUSH);
    swtButton.setText("SWT Button");
    final FXCanvas fxCanvas = new FXCanvas(shell, SWT.NONE) {

        public org.eclipse.swt.graphics.Point computeSize(int wHint, int hHint, boolean changed) {

            getScene().getWindow().sizeToScene();
            int width = (int)getScene().getWidth();
            int height = (int)getScene().getHeight();
            return new org.eclipse.swt.graphics.Point(width, height);
        }
    };
    Group group = new Group();
    final javafx.scene.control.Button jfxButton = new javafx.scene.control.Button("JFX Button");
    jfxButton.setId("ipad-dark-grey");
    group.getChildren().add(jfxButton);
    Scene scene = new Scene(group, javafx.scene.paint.Color.rgb(shell.getBackground().getRed(), shell.getBackground().getGreen(), shell.getBackground().getBlue()));
    scene.getStylesheets().add("twobuttons/Buttons.css");
    fxCanvas.setScene(scene);
    swtButton.addListener(SWT.Selection, new org.eclipse.swt.widgets.Listener() {

        public void handleEvent(org.eclipse.swt.widgets.Event event) {

            System.err.println("Hello from " + Thread.currentThread().getId() + ", " + Thread.currentThread().getName());
        }
    });
    jfxButton.setOnAction(new javafx.event.EventHandler<javafx.event.ActionEvent>() {

        public void handle(javafx.event.ActionEvent event) {

            System.err.println("Hello from " + Thread.currentThread().getId() + ", " + Thread.currentThread().getName());
        }
    });
    shell.open();
    while(!shell.isDisposed()) {
        if(!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

}

0 个答案:

没有答案