0

I am using NetBeans and have two classes under the same package (main.java uses a swing container with a button. FXapp.java uses JavaFX to play media in a separate window). Clicking the button on main.java is supposed to launch FXapp.java in its own thread. However, using the code below I get the app to launch but my java app hangs. Any suggestions on how to launch JavaFX apps without this problem? Also (maybe related) I would then like the Java app to be able to pass values to show in a text box on the FXapp. This may be realted in that I am unable to get a handle to the FXapp.

Java Button code:

FXApp xvp = new FXapp();

The main method code in FXapp is:

public static void main(String[] args) throws Throwable {
    FXapp obj1 = new FXapp();
    new Thread(obj1).run();
    setRefObj(obj1);
}

The FXapp "run" method code:

public void run() {
    try {
        Application.launch();
    } catch (Throwable t) {
    }
}
wchargin
  • 15,589
  • 12
  • 71
  • 110
  • what does `setRefObj(obj1)` do? How are you interrupting the thread once the job is done? Are you getting any exceptions? – Bizmarck Mar 25 '13 at 21:26

1 Answers1

0

JavaFX manages it's own threads, you should not try to specify a thread for JavaFX to use.

Rather than trying to launch a JavaFX application by invoking the Application.launch method from your Swing application, I advise using a JFXPanel to hold a scene created by a JavaFX application instance:

JFrame frame = new JFrame("Swing and JavaFX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final FXApp xvp = new FXapp();
final String initialText = "xyzzy";

Platform.runLater(new Runnable() {
  @Override public void run() {
    // createRootScene is a new public method you write and expose on FXApp.
    Scene scene = xvp.createRootScene(String customText);
    fxPanel.setScene(scene);
  }
});

The above method is detailed in the JavaFX for Swing Developers tutorial on integrating JavaFX into a Swing application.

When you make use of a JFXPanel, the JavaFX system will use a JavaFX Application thread managed by the JavaFX system and Swing will use it's Event Dispatch Thread. The JavaFX Application Thread will differ from the Swing Event Dispatch thread (as mentioned in the JavaFX documentation I linked). Care must be taken that all JavaFX processing is done on the JavaFX application thread (by using Platform.runLater constructs) and Swing processing is done on the Swing Event Dispatch thread (by using SwingUtilities.invokeLater constructs).

For your specific case of using JavaFX to play media from within an existing Swing application, there is a sample solution in the answer to the question: Playing audio using JavaFX MediaPlayer in a normal Java application? Note that to accomplish this you don't really need to create a new JavaFX application, but rather just create a JavaFX Scene which you can place inside a JFXPanel.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406