1

Hi i need to upgrade GUI from SWT to JavaFX and have problem straight at the beginning. The main class inherits from Thread and is being run from a certain method (interface implementation):

@Override
public void mStart() {


    this.start();

}

@Override
public void mStop() {

    this.shell.dispose();

}

My FX GUI is created with SceneBuilder. Question is how to manage Controller to start in a different thread as it is instantiated automatically ?

Baz
  • 36,440
  • 11
  • 68
  • 94
wieczors
  • 341
  • 1
  • 5
  • 18

2 Answers2

4

After some research the conclusion is: JavaFX app can be run as a different thread from non FX app, just need to implement Runnable and insert launch() method into run(). Then offcourse do start() on thread that is responsible to launch your GUI. Then if want to update your controls (buttons, labels, panels etc) from within different thread you must do something like this:

Platform.runLater(new Runnable() {
      @Override public void run() {
        //Update UI here     
      }
    });

for more information read posts: here and here

Community
  • 1
  • 1
wieczors
  • 341
  • 1
  • 5
  • 18
1

Well, I never used the Scene Builder nor SWT, but one thing I'm sure:

The JavaFX application thread is responsible for taking care of any existing JavaFX element. You can find details on how to properly handle the JavaFX elements here. What I think you have to do is make sure to access JavaFX elements using the JavaFX application thread, which you can do using the method runLater from Platform class. Take a look:

http://docs.oracle.com/javafx/2/api/javafx/application/Platform.html#runLater%28java.lang.Runnable%29

As always, we know we should start JavaFX applications from the main thread of a Java program. What you can do is to use the main thread in any point of your SWT application to call the initiator of your main JavaFX class. For example, at one point of your SWT application, all you have to do is call launch.

Do not forget, however, that the main thread (the one that should call launch) will be locked until your JavaFX application is finalized.

Hope this helps, and good luck. :)

Loa
  • 2,117
  • 3
  • 21
  • 45
  • yes it helped alot Loa. But exactly as you wrote above my main thread is being locked after invoking launch method. What if I would implement runnable on my main FX class and run launch from start() method on separate Thread? – wieczors Jan 17 '14 at 07:24
  • You mean, start a JavaFX application from another thread than the JavaFX Application Thread? I would not recommend it. It is clearly stated in the JavaFX documentation on the link that I passed you that the different threads of the Java platform have each its proper purpose. AH! Found it! Check this out: http://docs.oracle.com/javafx/2/swt_interoperability/jfxpub-swt_interoperability.htm – Loa Jan 17 '14 at 13:47
  • I meant to start whole JavaFX app in a separate thread than the main one which isn't the FX thread at all. Now what I need to do is to switch from SWT to JavaFX and make it run without need to do big changes to the rest of the project. But the problem is that in the main SWT class there was a while loop that was running for the whole apps life cycle (after initializing buttons, labels etc). With the FX approach there is a problem after running primaryStage.Show() method as it locks the rest of the flow. – wieczors Jan 20 '14 at 06:54