Java Reference
In-Depth Information
Listing 11.7
JavaFX Reflection - Function Invocation
// call Point2D.toString():String;
FXFunctionMember func =
classRef.getFunction(" toString ");
FXValue val = func. invoke (obj);
System.out.println(val.getValueString());
JavaFX and Java Threads
JavaFX is not thread safe and all JavaFX manipulation should be run on the JavaFX
processing thread. To ensure that interactions from Java code to JavaFX objects are
safe, you need to run the Java code that manipulates JavaFX objects on the main
JavaFX thread. If JavaFX is invoking the Java method, you probably are already on
the main processing thread. However, for other conditions, like remote procedure
callbacks, this may not be true. In the next chapter, JavaFX Code Recipes, we
present an example of how to do this using the Java Message Service (JMS) API.
If you are not on the main processing thread, with JavaFX 1.2, you need to use the
com.sun.javafx.runtime.Entry.deferAction() method. However, the way to
do this is expected to change in future releases to use a public API. Still, the new
way will be similarly designed to use a Runnable . For example:
import com.sun.javafx.runtime.Entry;
Entry.deferAction(new Runnable() {
public void run() {
manipulateJavaFX();
}
});
private void manipulateJavaFX() {
...
}
If you are a Swing programmer, this is similar to using the SwingUtilities
.invokeLater() method. However, the JavaFX main thread is not necessarily the
EventDispatchThread that is used in Swing. This is especially true for devices that
do not use Swing, like JavaFX mobile and JavaFX TV. Therefore, use the Entry
.deferAction() method for moving tasks onto the JavaFX main thread and do not
use the SwingUtilities methods.
For parameterized functions, locate the function using the function name and
parameter types. When invoking this kind of function, pass in the object as the
first argument followed by the parameter objects wrapped in mirrors. This is
shown in Listing 11.8.
 
Search WWH ::




Custom Search