0

I'm trying to remove a Rectangle from my window if it is moved to be inside of a certain part of the screen.

This is the error that I got:

Exception in thread "Thread-1539" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-1539 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:238) at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:400) at javafx.scene.Parent$1.onProposedChange(Parent.java:245) at com.sun.javafx.collections.VetoableObservableList.remove(VetoableObservableList.java:172) at com.sun.javafx.collections.ObservableListWrapper.remove(ObservableListWrapper.java:263) at com.sun.javafx.collections.VetoableObservableList.remove(VetoableObservableList.java:179) at MovementSample$HandListener.onFrame(MovementSample.java:136) at com.leapmotion.leap.LeapJNI.SwigDirector_Listener_onFrame(LeapJNI.java:495)

This is the snippet of code that cause the issue:

if(areOverlapping(sauceRectangle, pizzaInside)) {
                if(isHolding == null) {
                    Group g = (Group) scene.getRoot().getChildrenUnmodifiable().get(1);
                    g.getChildren().remove(sauceRectangle);
                }
            }

where areOverlapping() is just a method that checks some logic - the issue isn't there.

My question is this: How do I remove a rectangle from my screen if I have the scene. Also, what did I do wrong in my code?

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • The error says it `IllegalStateException: Not on FX application thread`. You are trying to do an operation which should be done on JavaFX Application thread and you are not on it, may be you have created a new thread and trying to operate the rectangle on it – ItachiUchiha Oct 07 '14 at 19:23
  • Ah yes, that is the case. How do I connect those two threads? Or, in other words, how do I have the other thread do that? – Alex K Oct 07 '14 at 19:33
  • Added an answer for more clarity – ItachiUchiha Oct 07 '14 at 19:38

1 Answers1

2

The error says it

IllegalStateException: Not on FX application thread

You are trying to do an operation which should be done on JavaFX Application thread and you are not on it.

In order to execute things on JavaFX Application thread, surround them with Platform.runLater

Platform.runLater(new Runnable() {
    @Override 
    public void run() {
       //Code to be executed on JavaFX App Thread
    }
});

For more information on Modifying UI components in JavaFX

Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176