When a JavaFX panel is added and removed from a Swing application it can't be added any more. Execute the test below and press the "add/remove FXPanel" button multiple times - for some reason it works only once.
public class EmbeddedFXPanelTest extends JFrame
{
private JFXPanel fxPanel;
public static void main(String[] args) throws Exception
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new EmbeddedFXPanelTest();
}
});
}
public EmbeddedFXPanelTest()
{
fxPanel = new JFXPanel();
add(new JButton(new AbstractAction("Add FXPanel")
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
JButton b = (JButton)evt.getSource();
if (fxPanel.getParent() == null)
{
add(fxPanel);
b.setText("Remove FXPanel");
}
else
{
remove(fxPanel);
b.setText("Add FXPanel");
}
revalidate();
repaint();
}
}), BorderLayout.NORTH);
Platform.runLater(new Runnable()
{
public void run()
{
createScene();
}
});
setTitle(getClass().getSimpleName());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
}
private void createScene()
{
FlowPane p = new FlowPane(10, 10);
p.getChildren().add(new Button("FX Button"));
p.getChildren().add(new CheckBox("FX CheckBox"));
p.setStyle("-fx-background-color:yellow");
fxPanel.setScene(new Scene(p));
}
}
Platform#setImplicitExit(false) can be used to avoid the the issue.
Platform.setImplicitExit(false);
Platform.runLater(new Runnable()
{
public void run()
{
createScene();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.