JDK-8124729 : JFXPanels are not reuseable
  • Type: Bug
  • Component: javafx
  • Sub-Component: application-lifecycle
  • Affected Version: 7u21
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • Submitted: 2013-05-21
  • Updated: 2015-06-17
  • Resolved: 2013-08-28
Related Reports
Duplicate :  
Description
If a JFXPanel is created and added to a container more than once it does not render the second and subsequent times. For instance in the example below a JFXPanel that is displayed in a JDialog is shown correctly the first time the JDialog is created, but the second time just a grey box is shown.

public class TestJFXPanelFailure {
    private static JFXPanel jfxPanel = new JFXPanel();
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JButton b = new JButton("Show Popup");
                b.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JDialog popup = new JDialog(frame, "Popup", true);
                        popup.add(jfxPanel);
                        popup.setSize(new Dimension(500, 200));
                        popup.setVisible(true);
                    }
                });
                JPanel panel = new JPanel();
                panel.add(b);
                frame.add(panel);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        FlowPane pane = FlowPaneBuilder.create().children(
                                new Label("Label 1"),
                                TextFieldBuilder.create().prefColumnCount(10).build(),
                                new Label("Label 2"),
                                TextFieldBuilder.create().prefColumnCount(10).build()).build();
                        Scene scene = SceneBuilder.create().root(pane).build();
                        jfxPanel.setScene(scene);
                    }
                });
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

If a static instance of the JDialog is just created once and hidden on close and set visible on show (as below) it displays fine so must be something to do with adding it to the container a second time.

public class TestJFXPanelSuccess {
    private static JFXPanel jfxPanel = new JFXPanel();
    private static JDialog popup = null;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JButton b = new JButton("Show Popup");
                b.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(popup == null) {
                            popup = new JDialog(frame, "Popup", true);
                            popup.add(jfxPanel);
                            popup.setSize(new Dimension(500, 200));
                            popup.setVisible(true);
                            popup.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
                        }
                        if(!popup.isVisible()) {
                            popup.setVisible(true);
                        }
                    }
                });
                JPanel panel = new JPanel();
                panel.add(b);
                frame.add(panel);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        FlowPane pane = FlowPaneBuilder.create().children(
                                new Label("Label 1"),
                                new Label("Label 1"),
                                new Label("Label 1"),
                                TextFieldBuilder.create().prefColumnCount(10).build()).build();
                        Scene scene = SceneBuilder.create().root(pane).build();
                        jfxPanel.setScene(scene);
                    }
                });
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

Comments
This is not an issue about JFXPanel, it's related to FX app lifecycle. When the last FX window is closed (and it can be an embedded window used by JFXPanel), FX is terminated and is never up again. This functionality is described in RT-23752 and is not currently targeted to FX 8. A simple workaround exists, though: Platform.setImplicitExit(false).
28-08-2013

While we're investigating this issue, could you try to run the test with the latest JavaFX 8 builds, please?
21-05-2013