I'm trying to build "Stick Hero" board game using javafx (JDK8). I use
scene.setOnKeyPressed(e -> {
Thread thread = new Thread() {
@Override
public void run() {
if (e.getCode().equals(KeyCode.ENTER)) {
// do some graphical changes
playGame();
}
}
};
thread.start();
});
to listen to keyboard and by pressing enter the vertical line turns into a horizontal bridge and the human pass the bridge and this will continue until the length of bridge doesn't match the distance. Now i want to update UI during playGame()
method is running. When i tried to do scoreLabel.setText(String.valueOf(score));
and update the scoreLabel
text i encountered Not on FX application thread
error. playGame()
is something like
for (int i = 0; i < cycle; i++) {
goOneCycle();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
fixPosition();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
and in goOneCycle();
and fixPosition();
i do some graphical changes but it does not shown until the end of the loop.
Now i have two questions. First why i can do stuff like scoreLabel.setLayoutX(500);
but i cannot do scoreLabel.setText(String.valueOf(score));
i mean in the first case i do not encounter Not on FX application thread
. And how i can update scoreLabel
text. Notice that i already tried to use Platform.runLater()
but when i use this animations and graphical changes will not shown in playGame()
method, and just the final frame is shown -all of the animations is done but it does not shown.