Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem updating main thread JavaFX control from custom Task

 
Michael D Sims
Ranch Hand
Posts: 70
1
Chrome Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I created this little program to basically be a countdown timer so that when I'm cooking, and I'm in the other room and not within earshot of the oven timer, I can set a timer and hear an alarm when the countdown reaches zero. Ultimately I want to be able to add tabs to the form as the program is running and add multiple timers so that when I'm cooking multiple things, I can set the minutes accordingly.

When I set up a concurrent task under a standard, every day run of the mill class, instantiating the Task from within the class itself, I have no problem binding the textProperty of the Controllers label, and updating its text via the updateMessage method of the Task ... but when I establish the class as a custom class that extends Task, and then instantiate it from the Controller, it doesn't seem to matter if I bind the textProperty from the Controller once the class is instantiated, OR if I bind it from within the class itself during initialization. It simply WILL NOT update that damn label.

I have the whole project on git under

EasyG0ing1/CountdownTimer

If you'd like to run it yourself and see what I'm talking about.

Any input would be welcomed.

Thank you,

Michael Sims
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That github link would be:
https://github.com/EasyG0ing1/CountdownTimer
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call run() on the task explicitly - that will just run the task on the current thread (in your case the JavaFX application thread), which will block all UI processing until the task is complete.

Instead create a thread, passing the task into the new thread and invoking start on the thread (which will implicitly invoke the run method for you).

There is info in the task documentation:
http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html

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

A little advice. Now you have concurrent things going on, just be careful that you manage them so that in your UI it is only possible to kick off a single countdown timer at a time, or if you kick off multiple countdown timers, provide their feedback into different labels otherwise the multiple concurrent activities will overwrite each other's countdown progress in the UI (you probably already do that, I didn't check the code in that respect, just something to keep in mind). If you have multiple timers then you can optionally used the ExecutorService facilities to help co-ordinate them, but, if the straight thread creation code provided above works fine for you, just stick with that are there a few more things you would need to worry about with an executor service such as co-ordinating shutdown of the service (it doesn't shutdown automatically, unlike a started thread which just terminates when done).

Also you might find some examples of countdown services on stack overflow if you search JavaFX countdown or JavaFX timer.
 
Michael D Sims
Ranch Hand
Posts: 70
1
Chrome Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Damien Smith wrote:Don't call run() on the task explicitly - that will just run the task on the current thread (in your case the JavaFX application thread), which will block all UI processing until the task is complete.

Instead create a thread, passing the task into the new thread and invoking start on the thread (which will implicitly invoke the run method for you).

I played around with this for a while, but I decided instead to just put the task in the Controller class and invoke it from there.

See updated code here

Next, I need to figure out how to make this program have the ability to add more tabs on the fly to add more timers. But, I’ll start a new thread for that.

Thank you very much for taking the time to reply to my question.

Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic