Swing Content breaking in FX TabPane

Viewed 119

I am working on a project that requires loading already existing complex Swing Content inside a UI built in FX. The problem i get is, that the Swing Content constantly "breaks" leaving the corresponding Swing Node / FX Pane empty. I have built a minimal working code example to partly reproduce the problem. When resizing the window containing 3 Tabs, the only Swing Node that shows properly is the one in the currently selected tab. Usually both other tabs "break".

public class Main extends Application {

  private final SwingNode node1 = new SwingNode();
  private final SwingNode node2 = new SwingNode();
  private final SwingNode node3 = new SwingNode();

  @Override
  public void start(Stage primaryStage) {
    try {

      TabPane root = new TabPane();
      Tab tab1 = new Tab("Tab 1");
      Tab tab2 = new Tab("Tab 2");
      Tab tab3 = new Tab("Tab 3");

      createAndSetSwingContent();

      tab1.setContent(node1);
      tab2.setContent(node2);
      tab3.setContent(node3);

      root.getTabs().add(tab1);
      root.getTabs().add(tab2);
      root.getTabs().add(tab3);

      Scene scene = new Scene(root, 400, 400);
      primaryStage.setScene(scene);
      primaryStage.show();
      
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void createAndSetSwingContent() {

    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        JPanel panel1 = new JPanel(new BorderLayout());
        JPanel panel2 = new JPanel(new BorderLayout());
        JPanel panel3 = new JPanel(new BorderLayout());

        JButton btn1 = new JButton("Button 1");
        JButton btn2 = new JButton("Button 2");
        JButton btn3 = new JButton("Button 3");


        panel1.add(btn1);
        panel2.add(btn2);
        panel3.add(btn3);

        node1.setContent(panel1);
        node2.setContent(panel2);
        node3.setContent(panel3);
      }
    });
  }

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

Is this problem reproducable for anyone else? And is this a java bug, or am i missing something in my code? I am using Java 11 and JavaFX 11 in eclipse. I included the JavaFX11 jars as a User Library in the Classpath, and added the following VM Arguments in Run Configurations:

--module-path "C:\JavaFX\javafx-sdk-11.0.2\lib" --add-modules javafx.controls,javafx.fxml,javafx.swing
1 Answers

Reminds me on an issue I had some years ago with JFXPanel - have you already tried Platform#setImplicitExit(false)?

Example:

Platform.setImplicitExit(false);
Platform.runLater(new Runnable(){
  public void run(){
    createScene();
  }
});
Related