1

I would need help with the following: I am implementing an application in javafx, this application Is called through a click on a button. The problem is that when I close the application then I can not call it again. I have read that you can not call the Application.launch() method more than once. But I found something on the service class. The examples in the documentation page are not very clear. Anyone have an idea of ​​how this could be done? Thank you.

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

my code:

private void jButton1ActionPerformed (java.awt.event.ActionEvent evt) {

      WebMap n1 = new WebMap () / / application in javafx

      n1.Lunch ();
}

WebMap class: / / javafx application

public void Lunch () {
     Application.launch ();
}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186

2 Answers2

2

You can't launch a JavaFX application more than once in the same process, so don't try to do that.

You need to find an alternative mechanism to do whatever it is you are trying to do.

If you are embedding JavaFX scenes in a Swing application you should be creating new JFXPanels in Swing, not creating new JavaFX Applications on Swing button presses.

If you are intending to have a pure JavaFX application, then there is no need to have a Swing button which launches a JavaFX application, you can just use a JavaFX button instead and directly display the JavaFX scene.

There is no need to use a Service in this case, Service is used for performing repeated background tasks on a another thread, which has nothing to do with what you are attempting.

Read JavaFX for Swing developers if you want to integrate a Swing and JavaFX application.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
0

As a committer of the com.bitplan.javafx open source project I can point you to the work-around we have been using for a while now:

https://github.com/BITPlan/com.bitplan.javafx/blob/master/src/main/java/com/bitplan/javafx/WaitableApp.java

 WaitableApp.toolkitInit();

will initialize the JavaFX environment.

https://github.com/BITPlan/com.bitplan.javafx/blob/master/src/main/java/com/bitplan/javafx/SampleApp.java

will show an example of how the WaitableApp base class is used in general. You also might want to have a look at the Junit Testcases of the project.

WaitableApp

/**
 * Waitable Application that does not need launch
 * 
 * @author wf
 *
 */
public abstract class WaitableApp extends Application {
  protected Stage stage;
  static boolean toolkitStarted;

  /**
   * allow startup without launch
   */
  @SuppressWarnings("restriction")
  public static void toolkitInit() {
    if (!toolkitStarted) {
      toolkitStarted = true;
      // do not exit on close of last window
      // https://stackoverflow.com/a/10217157/1497139
      Platform.setImplicitExit(false);
      /// https://stackoverflow.com/a/38883432/1497139
      // http://www.programcreek.com/java-api-examples/index.php?api=com.sun.javafx.application.PlatformImpl
      com.sun.javafx.application.PlatformImpl.startup(() -> {
      });
    }
  }

  @Override
  public void start(Stage stage) {
    this.stage = stage;
  }

  public Stage getStage() {
    return stage;
  }

  public void setStage(Stage stage) {
    this.stage = stage;
  }

  /**
   * wait for close
   * 
   * @throws InterruptedException
   */
  public void waitStatus(boolean open) {
    int sleep = 1000 / 50; // human eye reaction time
    try {
      if (open)
        while ((stage == null) || (!stage.isShowing())) {
          Thread.sleep(sleep);
        }
      else
        while (stage != null && stage.isShowing()) {
          Thread.sleep(sleep);
        }
    } catch (InterruptedException e) {
      ErrorHandler.handle(e);
    }
  }

  public void waitOpen() {
    waitStatus(true);
  }

  public void waitClose() {
    waitStatus(false);
  }

  /**
   * show me
   */
  public void show() {
    // ignore multiple calls
    if (stage != null)
      return;
    Platform.runLater(() -> {
      try {
        this.start(new Stage());
      } catch (Exception e) {
        ErrorHandler.handle(e);
      }
    });
  }

  /**
   * close this display
   */
  public void close() {
    Platform.runLater(() -> {
      if (stage != null)
        stage.close();
    });
    this.waitClose();
    // allow reopening
    stage = null;
  }
}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186