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);
}
});
}
}