简体   繁体   中英

JavaFX: Task will not update UI

I'm working on an Omaha online poker client written in javaFX+java.

I need to show an anchorPane containing 3 buttons after control.call() finishes executing. I know for a fact that control.call() finishes executing but for some reason task.setOnSucceeded(new EventHandler() { 's handle method does not update the User interface.

What am I doing wrong?

public void newRound() {

    sog = StateOfGame.PREFLOP;       

     ControlGameOnNewThread control = new ControlGameOnNewThread();

    Task task = new Task() {

        @Override
        protected Object call() throws Exception {
            control.call();
            return null;
        }
    };

    task.setOnSucceeded(new EventHandler() {

        @Override
        public void handle(Event event) {
            if (client.getAction() == FirstToAct.me) {
                System.out.println("Task finsished");
                    showOptions(client.getToCall());
                    opponentBetField.setText(new Integer(opp.chipsInvested).toString());
                    myBetField.setText(new Integer(client.chipsInvested).toString());
                    System.out.println("Task finsished");

            }
    }
    });


    new Thread(task).start();


}

The problem is that you are updating the user interface in the other thread.. if you are in the other thread and you want to update the user interface

You need to call the


Platform.runLater(new Runnable() {
  @Override public void run() {
    //Update UI here     
  }
});

calling this will call the main thread and update all the necessary update to the user interface

EDIT

public void newRound() {

    sog = StateOfGame.PREFLOP;       

     ControlGameOnNewThread control = new ControlGameOnNewThread();

    Task task = new Task() {

        @Override
        protected Object call() throws Exception {
            control.call();
            return null;
        }
    };

    task.setOnSucceeded(new EventHandler() {

        @Override
        public void handle(Event event) {

            if (client.getAction() == FirstToAct.me) {

                Platform.runLater(new Runnable() {
                  @Override public void run() {
                    System.out.println("Task finsished");
                    showOptions(client.getToCall());
                    opponentBetField.setText(new Integer(opp.chipsInvested).toString());
                    myBetField.setText(new Integer(client.chipsInvested).toString());
                    System.out.println("Task finsished");     
                  }
                });
            }
    }
    });


    new Thread(task).start();


}

As you override the call() method, you can override the succeeded() method: (See last example in the javadoc of Task.)

@Override protected void succeeded() {
     super.succeeded();
     updateMessage("Done!");
}

succeeded() is already called on the JavaFX Application Thread, so you do not need to do this for yourself.

Are you sure, that the code in your handle method isn't called? Maybe a NullPointerException is thrown, which you might not see? Is the comparison working as expected?

Try moving the code with if (client.getAction()... into the overridden succeeded() method and put a println before the if-statement in order to see whether it is called or not.

(EDIT: typos)

Your information has been helpful, but i think all methods suggested would have worked (including my initial one). The problem is that the task was never ended that's why the onTaskSucceded was never executed. In order to have the task exit after it has finished, i had to se the daemon to true:

Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM