Java Reference
In-Depth Information
There's more...
Wrapping custom Swing controls into JavaFX node
What if you wanted to use an existing custom Swing component as part of your JavaFX
application? Well, JavaFX provides an easy way to transform existing Swing components
into JavaFX node instances, which can be graphed into the scene using a script-level utility
function SwingComponent.wrap(swing:javax.swing.JComponent) . For instance:
var mySwingComponent = new CustomSwingComponent();
var fxNode = SwingComponent.wrap(mySwingComponent);
When using the SwingComponent.wrap() function, you should be aware of the following:
1. Your Swing component must be a descent of the JComponent class.
2. You lose the JavaFX idioms, such as class properties, function variables, and so on.
You must use Java-style getter and setters accessors to access instance values, and
you cannot use function variables to handle events.
Creating a Swing control façade from JavaFX
As we have seen previously, it is a rather easy exercise to wrap existing Swing components
using the SwingComponent.wrap() function. A more elegant way is to wrap your custom
Swing controls yourself by encapsulating them within a JavaFX façade class. The benefit
of this approach is having full control over the way your Swing controls are initialized and
exposed. Since you are wrapping your Swing components in a JavaFX class, you will be
able to retain the JavaFX idioms for creating and managing object instances.
The (very) abbreviated code snippet next shows you how this can be done by extending a class
SwingComponent and implementing function createJComponent():JComponent .
class PopupMenu extends SwingComponent {
var button:JButton;
var menu:JPopupMenu;
...
override function createJComponent (): JComponent {
button = new JButton();
menu = new JPopupMenu();
menu.setBorderPainted(true);
button.addActionListener(ActionListener{
public override function
actionPerformed(e:ActionEvent) {
menu.show(button, 0,button.getHeight()-5);
}
});
button;
}
...
 
Search WWH ::




Custom Search