I am starting a JavaFX gui application from another class, namely StartClient
by doing.
public class StartClient extends Application {
private Table gui;
@Override
public void start(Stage stage) throws Exception {
gui = new Table();
gui.start(stage);
I start a Task
through which I connect to a server and receive the turns assigned by the server, which I set in the gui using Platform.runLater
.
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
connectToServer(); // connect to server, set up socket, i/o streams
Object read = inputStream.readObject();
if (read instanceof String) {
turn = //parseInt from read
Platform.runLater(new Runnable() {
public void run() {
gui.setPlayerID(turn);
}
});
}
}
}; //end of task
What my problem is that I want to get the move made by a player if it is their turn and send it back to the server doing something like this:
if(networkClientID == gui.getState().getTurn()){
do {
action = Table.getAction(); //static getAction returns the move from the table if there was one
} while (action == -1);
outputStream.writeObject(action + ""); // write out turn
}
Do I do this on background Thread
(I am reading a static variable from the gui or should I do this in javaFX thread i.e. inside Platform.runLater
, I have tried it, but not getting anywhere, my program is getting stuck.)
Any suggestions, help, advise welcome on how to solve this problem. Thanks!