How to Fix Error: Not on FX Application Thread in Java

Sheeraz Gul Feb 02, 2024
How to Fix Error: Not on FX Application Thread in Java

This tutorial demonstrates the Not on FX application thread error in Java.

the Not on FX application thread in Java

The error Not on FX application thread occurs when we try to call a method from a thread that is not an FX thread and should be called from the FX thread. This was not a problem with previous versions of JavaFX.

It occurs with the new implementation of JavaFX 8. This error occurs when changing the user interface in different threads in the JavaFX application.

Here is an example that will throw the Not on FX application thread error.

Thread DemoThreadShow = new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      newthread.setStyle("visibility: true");
      Thread.sleep(10000);
      Thread.interrupted();
    } catch (Exception e) {
      thread.setText("" + Integer.valueOf(thread.getText()) + 5);
      newthread.setStyle("visibility: false");
    }
  }
});
DemoThreadShow.start();

The code above creates a thread and applies some methods to that thread. These methods are from the JavaFX application.

When applying the setText method, it will throw the error Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5.

The following changes can be made to the code to solve this issue.

  1. To make UI changes while working on FX applications and threads, use the following method.

    Platform.runLater(()
                          -> {
                              // GUI STUFF
                          });
    
    or :
    
        Platform.runLater(new Runnable() {
          @Override
          public void run() {
            // GUI STUFF
          }
        });
    
  1. We can also use the Service and Task rather than the Thread. Service has many more features than Thread.

    Changing the UI inside the task will never throw the Not on FX application thread error. Here is a code example of using Service and Task for this purpose.

    Service New_Service = new Service() {
      @Override
      protected Task createTask() {
        return new Task() {
          @Override
          protected Object call() throws Exception {
            Platform.runLater(()
                                  -> {
                                      // GUI stuff here
                                  });
            return null;
          }
        };
      }
    };
    New_Service.start();
    

    The code above is a more convenient way than threads.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Thread

Related Article - Java Error