Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting java.lang.IllegalStateException "Not on FX application thread" on JavaFX?

I have an application that has a TableView that has an attached listener so it refreshes as soon as it detects a change, but the thing is that I´m getting java.lang.IllegalStateException: Not on FX application thread; currentThread = Smack Listener Processor (0). Here is my code:

/**  * This function resets the pagination pagecount  */ public void resetPage() {     try {         System.out.println("RESET");          int tamRoster = this.loginManager.getRosterService().getRosterList().size();         paginationContactos.setPageCount((int)(Math.ceil(tamRoster*1.0/limit.get())));         int tamEnviados = this.loginManager.getRosterService().getEnviadasList().size();         paginationEnviadas.setPageCount((int)(Math.ceil(tamEnviados*1.0/limit.get())));         int tamRecibidas = this.loginManager.getRosterService().getRecibidasList().size();         paginationRecibidas.setPageCount((int)(Math.ceil(tamRecibidas*1.0/limit.get())));     } catch (Exception e) {         e.printStackTrace();     } }  public void doSomething () {         this.loginManager.getRosterService().getRosterList().addListener(new ListChangeListener<RosterDTO>() {             @Override             public void onChanged(                     javafx.collections.ListChangeListener.Change<? extends RosterDTO> c) {                 // TODO Auto-generated method stub                 resetPage();                 while (c.next()) {                     if (c.wasPermutated()) {                         System.out.println("PERM");                     } else if (c.wasUpdated()) {                         System.out.println("UPD");                     } else {                         System.out.println("ELSE");                     }                 }             }          }); } 

Altough it enters the resetPage method, I get that exception. Why is this happening? How can I fix it? Thanks in advance.

like image 876
linker85 Avatar asked Jul 25 '13 06:07

linker85


People also ask

What causes Java Lang IllegalStateException?

What Causes IllegalStateException. The IllegalStateException is thrown when the Java environment or application is not in an appropriate state for the requested operation. This can occur when dealing with threads or the Collections framework of the java.

What is the 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.

Is JavaFX thread safe?

The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread.

What is JavaFX platform runLater?

runLater. public static void runLater(Runnable runnable) Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future. This method, which may be called from any thread, will post the Runnable to an event queue and then return immediately to the caller.


2 Answers

The user interface cannot be directly updated from a non-application thread. Instead, use Platform.runLater(), with the logic inside the Runnable object. For example:

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

As a lambda expression:

// Avoid throwing IllegalStateException by running from a non-JavaFX thread. Platform.runLater(   () -> {     // Update UI here.   } ); 
like image 114
Marc Avatar answered Oct 12 '22 10:10

Marc


JavaFX code allows updating the UI from an JavaFX application thread. But from the above exception message it says that it is not using FX Application thread.

One way you can fix is to launch an FX Application thread from the resetPage method and do the modifications there.

like image 23
MohamedSanaulla Avatar answered Oct 12 '22 10:10

MohamedSanaulla