0
Timer randomTimer = new Timer();
     randomTimer.schedule(new TimerTask() {
     @Override
     public void run() {

         IN HERE IS A LONG PROCESS OF ADDING A CIRCLE TO MY ARRAYLIST CALLED
         SHAPES WHICH HAS NO ERROR ON ITS OWN

         int size = Shapes.size();
         MyPane.getChildren().add(Shapes.get(size));
         }
    }, 0, 1000);

Every second i create a new circle and add it to my arraylist of circles called shapes. I need to them add it to MyPane to show it on the javafx window

However i get this error.

Exception in thread "Timer-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = Timer-0

I have tried to use other timing methods like ScheduledExecutorService but haven't been able to pass in my array list. This timing method allows me to make new circles and add them to my array but i cant show them on my screen.

Thnaks in advance for any help

0

1 Answer 1

1

Thats because you try to modify javafx Nodes in non-javafx Threads (as the error description says). I think the following will fix the problem.

 Timer randomTimer = new Timer();
 randomTimer.schedule(new TimerTask() {
     @Override
     public void run() {
           // do your stuff including the next
           int size = Shapes.size();
           Platform.runLater(() -> {
               MyPane.getChildren().add(Shapes.get(size));
          });
     }
}, 0, 1000);

Basically the Platform.runLater() will run the given runnable on thr JavaFX-Main Thread. As it is the mainThread you should keep the operations as small as possible(as shown above) to prevent moments where your ui turns non-responsive.

Not the answer you're looking for? Browse other questions tagged or ask your own question.