Java Reference
In-Depth Information
var myFXcomponent = SwingComponent.wrap(myJavaJComponent);
This merely allows the Swing component to participate in the JavaFX scene
graph, but does not provide any other functionality. For instance, you do not have
any mapping of your JComponent 's attributes with corresponding JavaFX vari-
ables, so you do not realize any benefits from binding. If you wish to go one step
further, you need to implement a Custom Swing Component.
Custom Swing Component
As we mentioned, all JavaFX Swing classes extend javafx.ext.swing.Swing-
Component. These classes then must implement the function, createJComponent()
: javax.swing.JComponent . This is easy enough, but what we really want to do
is connect the Java Swing class's attributes to a JavaFX instance variable. Let's
work through an example using the JTextArea class.
First, our class extends SwingComponent and implements the createJComponent()
method to create the Swing component object.
public class TextArea extends SwingComponent {
override function createJComponent() : JComponent {
new JTextArea();
}
}
Next, add the helper function to cast the component returned by SwingComponent's
getJComponent() function to a JTextArea.
public function getJTextArea() : JTextArea {
getJComponent() as JTextArea;
}
Now, we are ready to add some attribute support. Let's start with JTextArea 's
text attribute, which is of course the text that is displayed on the screen.
public var text:String;
This is not enough though. What happens if the user types into the JTextArea ?
How does this field update? What if the program sets this value; how does the
JTextArea update? We need two more things. One is an on replace trigger so
that when the JavaFX text instance variable changes, a corresponding change is
made to the JTextArea . We will add an initialization to get the default value
from the JTextArea .
 
Search WWH ::




Custom Search