Issue
I'm having some trouble with getting ScheduledExecutorService to work.
This is the method that i'm calling which works if I call it without scheduling it.
However, if I call it through an ScheduledExecutorService my screen wont update, it also only runs one time, so I guess there's an exception being thrown, but I dont see an exception on either the server or client.
private void getNextRoundFromServer() throws IOException{
try{
if(!this.connected) return;
this.dos.writeByte(2);
this.dos.flush(); // Send data
this.grid = (Grid) this.ois.readObject(); //i have checked, i do recieve this object
setMainPane(render.render(this.grid)); //this renders the view
} catch(ClassNotFoundException ex){
ex.printStackTrace();
}
}
This is how I do my implementation.
continuousPlayButton.setOnMouseClicked(e -> {
try{
Runnable scheduledRound = () -> {
try {
getNextRoundFromServer();
} catch (IOException ex) {
ex.printStackTrace();
}
};
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(scheduledRound, 1, 5, TimeUnit.SECONDS);
}catch(Exception ex){
ex.printStackTrace();
}
});
Why isn't my view updating?
Update
Solution:
As mention by user tjanu
: I forgot to call Platform.runLater
on the scheduled task. Which I already did when I called it without scheduling... 😌
Solution
You need to do UI updates on the JavaFX application thread.
This is what Platform::runLater is for.
-- edit As requested, the change needed for this code:
private void getNextRoundFromServer() throws IOException{
try{
if(!this.connected) return;
this.dos.writeByte(2);
this.dos.flush(); // Send data
this.grid = (Grid) this.ois.readObject(); //i have checked, i do recieve this object
// This is the needed change
Platform.runLater(() -> setMainPane(render.render(this.grid))); //this renders the view
} catch(ClassNotFoundException ex){
ex.printStackTrace();
}
}
Answered By - tjanu