When JavaFX came out many thought it would be a replacement for Swing. However, the creators of JavaFX understood the importance of Swing to the Java desktop developer community. As a result, JavaFX allows developers to embed standard Swing components directly in JavaFX applications via the Swing extension API found in javafx.ext.Swing. Swing components are wrapped by JavaFX nodes so they can be added to the scene graph. When rendered, however, they produce a Swing component instead of a native JavaFX GUI control.
Let us take a look at some of the standard Swing component wrappers that are readily available to be used from JavaFX.
The following code snippet shows how to embed SwingLabel and SwingTextField. These wrapper component will add a JLabel and JTextField component, respective, to a JavaFX scene graph when rendered. Note that you can assign an id property to each component so it can be referenced in other part of the code.
The code snippet above would produce the fields shown in the following figure:
The Swing wrapper class SwingButton produces a JButton in the JavaFX. Similar to the native Button class, SwingButton can receive property action to specify callback handler function when the button is clicked.
The code snippet above produces a JButton as shown in figure below. When the button is clicked, the function assigned assigned to property action is executed.
The Swing wrapper classe SwingRadioButton creates JRadioButton class embedded in your JavaFX application.
The snippet above produces the result shown in the figure below. Notice the ToggleGroup instance used to group the button choices.
Checkboxes are created in a similar fashion using the SwingCheckBox class as shown below:
The code below shows how to display list of items using the Swing wrapper class SwingList. This produces a JList Swing component in the JavaFX application.
The code snippet above produces SwingList control as shown in figure below:
JavaFX provides many more Swing wrapper classes to bring Swing and JavaFX closer. They include: