2

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));
  }      
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
wzberger
  • 923
  • 6
  • 15
  • I've already reported as bug http://bugs.java.com/view_bug.do?bug_id=JDK-8113557 in 2011, but it's marked as fixed. – wzberger Dec 19 '15 at 15:51
  • Looks like some extra work is required - Platform.setImplicitExit(false) fixes the issue. – wzberger Dec 19 '15 at 16:26
  • 2
    Please add a [self-answer](http://stackoverflow.com/help/self-answer) with the implicit exit solution and mark it correct. – jewelsea Dec 19 '15 at 17:03

1 Answers1

3

Platform#setImplicitExit(false) can be used to avoid the the issue.

Platform.setImplicitExit(false);
Platform.runLater(new Runnable()
{
  public void run()
  {
    createScene();
  }
});
wzberger
  • 923
  • 6
  • 15