Java Reference
In-Depth Information
Embedding Swing components in JavaFX
As explained in the Introduction of this chapter, the Swing GUI framework has evolved into
a set of rich GUI components, which developers have come to love over the years. Suppose,
however, you want to continue using Swing in your JavaFX application; what do you do? This
recipe shows you how to use the JavaFX wrapper APIs for Swing to embed Swing components
in JavaFX scripts.
Getting ready
The JavaFX Swing wrapper classes let developers embed Swing components directly into
JavaFX applications. The wrapper API can be found in the package javafx.ext.swing . For
this recipe, it is assumed that you are familiar with using Swing components. It will also be
helpful to be familiar with JavaFX's Control API. To illustrate how to embed Swing components
in JavaFX, we have converted the data form introduced in the recipe Creating a form with
JavaFX controls , to use all Swing components.
How to do it...
The next code snippet shows you how to embed Swing controls in JavaFX. Since this
example is based on the data form introduced earlier in the recipe Creating a form with
JavaFX controls , the code will be abbreviated down to the essentials. You can, however,
get a full listing of the code discussed here from ch04/source-code/src/controls/
DataFormWithSwing.fx .
def rdoBtns = SwingToggleGroup {};
var nameRow = HBox {spacing:7
content:[
VBox{content:[ SwingLabel {text:"First Name"},
SwingTextField { id :"fName" columns:10}]}
VBox{content:[ SwingLabel {text:"Last Name"},
SwingTextField { id :"lName" columns:10}]}
]
}
...
var titleRow = HBox {spacing:7
content:[
HBox{
nodeVPos:VPos.CENTER spacing:7
content:[
SwingLabel{text:"Title:"}
SwingRadioButton {
text:"Programmer"
toggleGroup: rdoBtns id:"pgmr"}
 
Search WWH ::




Custom Search