A Mutable Log

A blog by Devendra Tewari


Project maintained by tewarid Hosted on GitHub Pages — Theme by mattgraham

Use JavaFX in a Swing or console app

This short post explores how to integrate JavaFX into a legacy console or Swing application.

Assuming you’ve created an application such as the WebView Sample, create a separate thread to launch JavaFX’s Application class

Thread appThread = new Thread(() -> {
  launch();
});
appThread.start();

To allow us to control when JavaFX will exit, disable implicit exit. Implicit exit happens when the last window (Stage) is closed by calling hide() or close(). Add this snippet of code to the start() method to JavaFX’s Application class

Platform.setImplicitExit(false);

To run code on JavaFX Application thread

Platform.runLater(new Runnable() {
  @Override public void run() {
    // code runs on JavaFX thread
  }
});

To exit gracefully when legacy application exits

Platform.exit();