![]() |
The seamless integration of JavaFX Script with pure Java code is one of the most interesting features of JavaFX Script. Using Java objects within JavaFX Script is simple, because JavaFX Script was designed with this feature in mind and the necessary instruments have been build into the language. This article will present possible ways to create JavaFX objects and use them in Java code. ContentsWho Creates Whom?
When designing a system that includes components written in JavaFX Script as well as components done in pure Java language, one of the first questions that comes up is which technology will be responsible for the startup. There is no correct answer, as it depends on the context. The most common scenario is that the main application was written in JavaFX Script and uses Java libraries for the technical tasks. But there are also scenarios for the opposite approach, where code written in the Java language creates objects implemented in JavaFX Script. In a Java application, for example, the GUI layer may be migrated to JavaFX Script, while the businesslogic (including the startup procedure) is as unchanged as possible. In the first scenario, a component written in JavaFX Script that creates Java objects can be achieved by calling the constructor of the Java classes in JavaFX Script in much the same way as one would in a pure Java environment. The second scenario needs slightly more consideration. One approach could be to create JavaFX objects directly by using the constructors generated by the compiler. But this approach is discouraged, because it is generally not desirable to depend on implementation details of the compiler. Creating JavaFX Objects in Java
There are ways to create JavaFX objects from Java code without calling the constructor directly. The simplest solution is to use a factory, which is written in JavaFX Script. This solution has the advantage that the objects are created in JavaFX Script and therefore the complete range of possibilities allowed in object literals are available during the process of creation. How the methods of a factory written in JavaFX can be called from a Java application is described in the section below. Although this approach is simple and yet very powerful, there is a major drawback. For the very first object (such as the factory itself), a different solution needs to be implemented. The most flexible approach to create JavaFX objects is to use the Script engine that is part of the compiler. It allows you to parse and execute JavaFX Script on-the-fly. A script can be passed as a text file or as a string argument. The following example will pass a string argument to create a JavaFX object: It shows a simple JavaFX class with one attribute property and a single method, Code Sample 1: Definition of
MyJavaFXClass
The Java class in Code Sample 2 creates an instance of The first step is to create an instance of a The next two statements define the JavaFX Script to be executed. The script consists of an object literal, which creates an instance of Code Sample 2: Constructing
MyJavaFXClass in Java program
Working with JavaFX objects
Once the Java objects and the JavaFX objects are created one way or the other, communication between both worlds has to be implemented. Again there is the simple scenario (JavaFX Script calling Java methods) and a more challenging one. The Java compiler is not able to parse JavaFX Script and is therefore not able to extract the needed information from the sources. One might again be tempted to access the compiled Java classes directly. Although this is currently possible, it is not recommended for the same reason that the generated constructor should not be used directly. It would introduce a number of dependencies on the implementation details of the compiler. There is no guarantee that the compilation of JavaFX won't be changed in the future, therefore relying on the byte code that is currently created introduces some heavy risks in terms of compatibility. Another solution to access a method of a JavaFX object was already presented in the example above. The Implementing Java Interfaces
JavaFX classes are able to implement Java interfaces just as well as Java classes. This feature can be used to allow Java components to access JavaFX objects as they would access any other implementation of an interface. In fact the Java components do not even need to know that they are dealing with JavaFX objects under the hood. As an example, the method Code Sample 3: The interface
Printable
Next step is to make sure the class Code Sample 4:
MyJavaFXClass implementing a Java interface
With this change Code Sample 5: Working with JavaFX objects via interface
(Reflection)
A fourth alternative is the Reflection API of the JavaFX runtime. It will be accessible from JavaFX Script and from Java programs, and is currently under development. In the near future, it may provide another way to access JavaFX objects. Using JavaFX sequences in Java
Sequences are a fundamental data type in JavaFX. But they do not exist in the Java language and therefore were implemented as a new data structure. Currently there exist more than a dozen Java classes to implement JavaFX sequences. Which implementation(s) are used depends on the way the sequence was assembled. But all of the implementations share the same interface Creating sequences
The helper class Table 1: Relationship between Java arrays and created JavaFX sequences
The factory method Working with sequences
The interface The interface
References
About the Author
Michael Heinrichs is a software engineer on Sun's Java ME development team. | |||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() |
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|