JavaFX and SwingNode - partial black Window

2019-03-06 10:41发布

问题:

I tried to use Swing Components in JavaFX with the SwingNode:

public class MyTest extends Application {

    @Override
    public void start(Stage stage) {

        final SwingNode swingNode = new SwingNode();
        FlowPane pane = new FlowPane();

        Button btn = new Button("1");
        btn.setVisible(false);
        pane.getChildren().add(btn);

        createAndSetSwingContent(swingNode);
        pane.getChildren().add(swingNode);


        stage.setScene(new Scene(pane, 100, 50));

        stage.show();

        btn.setVisible(true);

    }

    private void createAndSetSwingContent(final SwingNode swingNode) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                swingNode.setContent(new JButton("Click me!"));
            }
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}

After starting the application some parts of the window (the background and the JavaFX button) are black:

After a resize (or an other update) the window is shown correctly:

Why is it like this and how can I fix it?

回答1:

The Problem is an known bug in java 8 update 31.

(https://javafx-jira.kenai.com/browse/RT-37810)

It is fixed in java 8 update 40.



回答2:

objSwingNode.requestFocus(); objSwingNode.autosize();