1

I want to change UI in other threads and tried this way-

SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            try {
                Thread.sleep(5000);
                 lblToast.setText(6+"");
            } catch (InterruptedException ex) {
                Logger.getLogger(FXMLDocumentController.class.getName())
                                                  .log(Level.SEVERE, null, ex);
            }
        }
 });

But this code do not work.

Spotted
  • 4,021
  • 17
  • 33
saman1046
  • 11
  • 1

3 Answers3

3

The basic facility to run something on the JavaFX application thread is Platform.runLater(). However, by your comments, you seem to also want to run the something on the JavaFX application thread after a delay, so that is what this answer addresses.


The code below will schedule to do something on the JavaFX application thread after a 5 second delay:

Platform.runLater(() -> {
    PauseTransition pause = new PauseTransition(Duration.seconds(5));
    pause.setOnFinished(event -> doSomething());
    pause.play();
});

In your case, doSomething() is:

lblToast.setText(6+"");

This is similar to the solution to:

A (minor) advantage of the PauseTransition over the use of ScheduledExecutorService is that the transition does not require an additional thread. A disadvantage is that the ScheduledExecutorService returns a ScheduledFuture which might give you a bit more control over the process as you can call methods like cancel() or isDone() on the ScheduledFuture (that extra control might not be important for your application though).

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • @saman1046 jewelsea is right about the advantage(s) of his solution. Furthermore I suspect you don't need the control provided by a `ScheduledFuture` like in my solution, so just stick with his solution (which is simplier). :-) – Spotted Mar 14 '16 at 12:32
2

ScheduledExecutorService.schedule() allows to execute a task after a specified delay. Platform.runLater() executes the Runnable on the JavaFX-Thread.

ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
Runnable setLabelOnUI = () -> Platform.runLater(() -> lblToast.setText(6+""));
ex.schedule(setLabelOnUI, 5000, TimeUnit.MILLISECONDS);
Spotted
  • 4,021
  • 17
  • 33
-2

Edit: Make sure the label variable is static so the Runnable Thread can access it! Heres the syntax:

Runnable displayRunnable = new Runnable(){
                        @Override
                        public void run(){
                        //Enter Code to Change UI here!

                        }
 };
//Display Runnable allows us to modify UI components
Display.getDefault().syncExec(displayRunnable);
  • If you're having a illegal thread exception, calling Display.getDefault().syncExec(Runnable r); is the way to go. I believe your question addressed inserting a timer. Just insert the timer code block inside of the function run. – Rifat Rashid Mar 14 '16 at 07:17
  • 3
    what is Display class? – saman1046 Mar 14 '16 at 07:28