How do I "restart" a JavaFX Application?


Question

I've been coding a simulation in Java and now generate graphs of the results in JavaFX. However, I'm stumped as to how to call the graphs so that when I try to run the simulation (and generate the graphs) for a second time, I don't get the "can't call launch() more than once" error. I read that launch() can only be called once, so what do I need to change to call the graphs successfully on a second run of the simulation?

public class AnswerWorker extends SwingWorker<Void, Integer> {

protected Void doInBackground() throws Exception
{
    AMEC.runsimulation();
    return null;
}

protected void done()
{
try {
    Graphs.launch(Graphs.class, "");
    JOptionPane.showMessageDialog(InputGUI.this, AMEC.unsuccesfulpercentage + "% of iterations had trucks that had to sleep over");
    AMEC.unsuccesfulpercentage = 0;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}
}

with a pretty standard graphing class:

public class Graphs extends Application {

    @Override public void start(Stage stage) {
    ....
    stage.show();
    }
}
1
0
1/15/2014 10:52:40 AM

Accepted Answer

Why are you using SwingWorker in a JavaFX application? If your graphs are computed for display in a JavaFX application, there is no reason for you to use the Event Dispatch Thread (AWT). If I'm not mistaken, With the release of JDK 8, the JavaFX Application Thread and the Event Dispatch Thread will become one, so there is no reason (yet) for you to use SwingWorker. If I really am wrong, still, there is no reason for you to use SwingWorker. Although it is possible the coupling of Swing components in JavaFX applications, you should only use EDT when manipulating Swing components, not JavaFX nodes. The following link says this:

...The JavaFX application thread is a different thread from the Swing and AWT Event Dispatch Thread (EDT), so care must be taken when embedding JavaFX code into Swing applications...

Source: http://docs.oracle.com/javafx/2/architecture/jfxpub-architecture.htm

To learn how you can put processes to be done in the background, and also find out which thread is responsible for handling the JavaFX elements, check out this link:

http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

Regarding your question, I believe the beginning of a JavaFX application should be called only once. So much is, that when a JavaFX application is started, the thread of main method is captured, and is only returned when the JavaFX application is finalized. Just see the Application DOC (Especially this part). Note also that the life cycle of a JavaFX application is given by the following steps:

Life-cycle

The entry point for JavaFX applications is the Application class. The JavaFX runtime does the following, in order, whenever an application is launched:

  1. Constructs an instance of the specified Application class
  2. Calls the init() method
  3. Calls the start(javafx.stage.Stage) method
  4. Waits for the application to finish, which happens when either of the following occur: the application calls Platform.exit() the last window has been closed and the implicitExit attribute on Platform is true
  5. Calls the stop() method

You might be using the wrong approach. I believe that you should leave your JavaFX application running while you're processing something. Once you have processed what you want in the background, you should make your JavaFX application show the results. The way your JavaFX application run while something is being processed in the background is entirely up to you. Maybe you should hide the window while something is processed, and display it again with your chart done. As I said, this is something you will have to decide. I recommend you to take a look at the documentation of classes referring to the window system, starting with Window, and then Stage.

Any questions, we're here. Good Luck! :)

EDIT:

From what I'm seeing, the user jewelsea is right. Apparently it is not certain that the EDT and the JavaFX Application Thread shall become one (it would be quite interesting if that happened). But anyway, be careful not to confuse yourself with the use of both threads.

2
1/16/2014 7:22:17 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon