Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JavaFX access gui button action from another thread

992276Feb 22 2013
I am starting a JavaFX gui application from another class, namely StartClient by doing.


public class StartClient extends Application {

private Table gui;

<pre>
@Override
public void start(Stage stage) throws Exception {
gui = new Table();
gui.start(stage);
</pre>

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.

<pre>
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
</pre>

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:

<pre>
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
}
</pre>
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!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 22 2013
Added on Feb 22 2013
0 comments
277 views