Reputation: 378
I use a Thread to change the opacity of an AnchorPane. I add an onMouseEnter method to raise opacity gradually,and an onMouseExited method to decrease opacity gradually. I have two panes, if my mouse enter any one of them,both two panes will change opacity. Then I start my application, I find that after my mouse enter and exit the pane for some times, one will stop at a specific opacity,while the other still change opacity gradually. I am confused why this happen... My codes are like:
private double base = 0.5;
private void changeOpacity(boolean increase){
new Thread(()-> {
while((increase && base < 0.9) || ((!increase)&&base > 0.5)){
base += increase?0.01:-0.01;
leftPane.setOpacity(base);
rightPane.setOpacity(base);
Thread.sleep(5);
}
}
}
I am sorry for my poor English. Thanks.!
Upvotes: 0
Views: 433
Reputation: 5897
You are only allowed to change values of JavaFX components while you are on the JavaFX-Thread (something you are clearly not).
You need to sync yourself back using Platform.runLater(Runnable)
but in your case you'd swap the event loop with Runnables which an equally bad idea.
The correct way is:
Upvotes: 2