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!

Understanding "not on FX application thread" / Platform.runLater

cshDec 15 2011 — edited Dec 16 2011
Hi,

I start a new Thread and in it I change some UI elements:
final Label label = new Label();

Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                    // do work
                    label.setText("text");
            }
});
thread.start();
The strange thing is, that on my machine the above code works, and on another machine it doesn't ("not on FX application thread").

I know that I could just wrap label.setText() with Platform.runLater(). But actually I want to know, why this code works at all on my machine?

Another scenario is when I don't access UI elements directly but use e.g. binding to update them. E.g. I update a StringProperty in my Thread, which is bound to a textproperty of a Label. Do I need to use runLater then too?

Is there a rule of thumb, when to wrap in Platform.runLater? Do I just have to know, when something triggers an UI update? But still, why does the above code at all (at least on my machine)?

Edit: I think on the other machine is JFX 2.0.2 (it was the JFX, which is bundled with Java JDK 7 Update 2), and on mine is 2.0.1.

Edited by: csh on 15.12.2011 01:18
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 13 2012
Added on Dec 15 2011
3 comments
2,332 views