I have a stream
Thread skillshowin = new Thread(new Runnable() {
@Override
public void run() {
try {
newskill.setStyle("visibility: true");
Thread.sleep(10000);
Thread.interrupted();
} catch (Exception e) {
skill.setText("" + Integer.valueOf(skill.getText()) + 5);
newskill.setStyle("visibility: false");
}
}
});
skillshowin.start();
But I get the error Exception in thread "Thread-5" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-5
when executing skill.setText
Answer 1, authority 100%
To modify UI
in other threads in JavaFX
, you must use other means:
Platform.runLater()
:
Option with lambda:
Platform.runLater(() -> {
//your code
});
Alternatively with Runnable
:
Platform.runLater(new Runnable() {
@Override
public void run() {
//your code
}
});
As well as Service
and Task
. The Service
is more powerful than the normal Thread
. Changing UI
inside Task
will not throw out Not on FX application thread
, but inside Task
you also need to write Platform.runLater()
, otherwise all sorts of errors may occur.
Service service = new Service() {
@Override
protected Task createTask() {
return new Task() {
@Override
protected Object call() throws Exception {
Platform.runLater(() -> {
//your code
});
return null;
}
};
}
};
service.start();