Sarah H comments on a blog post:
Hi again, you said "Also, JavaFX has reflection." sorry,but i didn't understand very well..can u make it a little bit more clear ? besides is there something similar to "Class.forName(str).getInstance();" but in JavaFX ? and who can also handle the properties like for example in the case of a SwingButton(text,action..) Thanks
Sarah (and anyone else that is curious about reflection in JavaFX),
That's a very good question, and there is very little documentation on this subject so far. Here's a quick example of using the reflection API in JavaFX. First a screenshot:
When you first invoke the program, only the button appears. Each time you click the button, an instance of a TextBox is created using classes in the javafx.reflect package. Each instance is added to the content sequence of a VBox layout container which causes each new TextBox to appear below the previous one. Take a look at the code below while browsing the javafx.reflect package in the JavaFX API docs to see what's going on in this program. Here's the code:
/*
* ReflectExample1Main.fx
*
* Developed 2009 by James L. Weaver as a very simple example of
* using the javafx.reflect package in JavaFX
*/
package javafxpert.reflectexample.ui;
import java.util.Date;
import java.util.Iterator;
import javafx.reflect.*;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.ext.swing.SwingButton;
Stage {
var sceneRef: Scene;
title: "Reflection"
scene:
sceneRef = Scene {
var vBoxRef:VBox;
width: 500
height: 500
content: [
SwingButton {
translateX: 10
translateY: 10
text: "Create TextBox"
action: function():Void {
// Now use reflection to create a TextBox object
// First, get the local context
var ctx:FXLocal.Context = FXLocal.getContext();
// Then, get an object that represents the class type
var cls:FXLocal.ClassType = ctx.findClass("javafx.scene.control.TextBox");
// Just for grins, print the variable names of the TextBox class
var varIter:Iterator = cls.getVariables(true).iterator();
while (varIter.hasNext()) {
println(varIter.next());
}
// Now, create an object that represents the TextBox object
var objVal:FXObjectValue = cls.allocate();
// Initialize variables of the object
var now:String = new Date().toString();
objVal.initVar("text", ctx.mirrorOf(now));
objVal.initVar("columns", ctx.mirrorOf(20.0));
// Initialize the TextBox object
objVal = objVal.initialize();
// Get the JavaFX reference to the TextBox
var node: Node = ((objVal as FXLocal.ObjectValue).asObject()) as Node;
// Insert this TextBox node into the VBox by appending it to
// the content sequence
insert node into vBoxRef.content;
}
},
vBoxRef = VBox {
translateX: 200
translateY: 10
spacing: 5
}
]
}
}
As you can see, the reflection API enables capabilities such as dynamic generation of forms based upon data structures known only at runtime. There will be more information about the JavaFX reflection API next week in the Pro JavaFX Platform book (which is available in eBook form through the Apress Alpha program).
Regards,
Jim Weaver
Recent Comments