Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?

Below code snippets is giving me error Not on FX application thread; currentThread=JavaFX Application Thread.This application was working fine in java 1.7 but when i moved it to fx8 it is now giving error. when I start the application on my 1st attempt it is working as intended .But after closing the stage and opening it again it is not working.

The error is also ambiguous Not On fx application thread and current thread- javafx application thread.What did it mean by not on fx application thread if the current thread is a fx application thread.

progressDialog = createProgressDialog(service); progressDialog.show(); progressDialog.setOnCloseRequest(new EventHandler<WindowEvent>() {     @Override     public void handle(WindowEvent event) {         // if (service.isRunning()) {         // service.cancel();         progressDialog.close();         // }     } }); 
@SuppressWarnings("unchecked") private Stage createProgressDialog(final Service<IStatus> service) {     stage = new Stage();      URL url = FileLocator.find(Activator.getDefault().getBundle(),     new Path("icons/xxx_16x16.png"), null); //$NON-NLS-1$     stage.getIcons().add(new Image(url.getFile()));     stage.setTitle("Downloading ..."); //$NON-NLS-1$     // Creating StackPane     stage.initModality(Modality.WINDOW_MODAL); } 
like image 570
Rajesh Kumar Dash Avatar asked Jan 13 '14 04:01

Rajesh Kumar Dash


People also ask

Why is JavaFX not thread safe?

Thread safety in a JavaFX application cannot be achieved by synchronizing thread actions. We must ensure that the programs that manipulate the scene graph must do so only from the JavaFX Application Thread. Therefore, multithreading in JavaFX has to be handled in a different manner.

What is JavaFX application thread?

JavaFX uses a single-threaded rendering design, meaning only a single thread can render anything on the screen, and that is the JavaFX application thread. In fact, only the JavaFX application thread is allowed to make any changes to the JavaFX Scene Graph in general.


2 Answers

Calling

Platform.runLater(new Runnable(){ // do your GUI stuff here }); 

will fix it.

like image 181
Martin Pfeffer Avatar answered Oct 09 '22 23:10

Martin Pfeffer


This happened with me when i was modifying UI element from task in javafx 2 like listview elements.A Task Which Modifies The Scene Graph helped me to solve the issue i.e. updating UI elements by

 final ListView<String> group = new ListView ();   Task<Void> task = new Task<Void>() {       @Override protected Void call() throws Exception {           group.getItems().clear();              for (int i=0; i<100; i++) {                              Platform.runLater(new Runnable() {                  @Override public void run() {                      group.getItems.add(i);                  }              });          }          return null;      }  }; 
like image 26
vinay Avatar answered Oct 09 '22 23:10

vinay