//fix this /
COMMUNITY
In the last issue, Jonathan Giles showed us
Java code that demonstrates a thread retrieving
from a remote datasource, and updates a JavaFX
ProgressBar control. He asked if this code was
guaranteed to work in all environments.
The correct answer is #3: No. The ProgressBar must always
be updated from the JavaFX application thread. Whenever you
use separate threads in JavaFX, you must always be sure to
update the scene only via the JavaFX application thread. This
is achieved by calling Platform.runLater(..) from within your
threading code. If this is not done, the user interface may not
appear as expected in all circumstances (and on all machines).
Future JavaFX releases may even make this a runtime exception,
so do the right thing now!
2 THE CODE
Consider the following code fragment for a JavaFX application:
...
IntegerProperty myInt = new SimpleIntegerProperty(5);
IntegerProperty myBoundInt = new SimpleIntegerProperty(0);
myBoundInt.bind(myInt);
myInt.set(8);
System.out.println("My bound integer's value is: "+ myBoundInt);
myBoundInt.set(5);
System.out.println("My bound integer's value is: "+ myBoundInt);JAVA IN ACTION
3 WHAT S THE FIX?
What will be the output when you try to run this code?
1) My bound integer's value is: 8 My bound integer's value is: 5
This issues challenge comes from Angela Caicedo, a Java
evangelist at Oracle.
2) My bound integer's value is: 5 My bound integer's value is: 5
ABOUT US
1 THE PROBLEM
One of the coolest features of JavaFX is binding
capabilities. When JavaFX properties participate in bindings,
changes made to one object are automatically reflected in
another object. This is particularly useful when creating
graphical user interfaces because you can automatically keep
the display synchronized with the underlying data.
3) My bound integer's value is: 8 My bound integer's value is: 8
4) My bound integer's value is: 8 Exception***
Hint: Read the JavaFX 2.0 documentation “Using JavaFX Properties and Binding.”
blog
GOT THE ANSWER?
Look for the answer in the next issue. Or submit your own code challenge!
ART BY I-HUA CHEN
64
ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2012