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. SwingLabel and SwingTextFieldThe 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. var nameRow = HBox { spacing:3 content:[ VBox{content:[SwingLabel{text:"First Name"},SwingTextField{id:"fName" columns:10}]} VBox{content:[SwingLabel{text:"Last Name"},SwingTextField{id:"lName" columns:10}]} ]}The code snippet above would produce the fields shown in the following figure: SwingButtonThe 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. SwingButton { text:"Click me, I am Button" font: Font {name:"Arial Black" size:18} effect:DropShadow{offsetY:3 offsetX:3} action:function():Void { Alert.inform("Ouch, you clicked me!"); }}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. SwingRadioButton & SwingCheckBoxThe Swing wrapper classe SwingRadioButton creates JRadioButton class embedded in your JavaFX application. def rdoBtns = SwingToggleGroup{};var titleRow = HBox { spacing:3 content:[ HBox{ nodeVPos:VPos.CENTER spacing:7 content:[SwingLabel{text:"Title:"} SwingRadioButton{text:"Programmer" toggleGroup:rdoBtns id:"pgmr"} SwingRadioButton{text:"Manager" toggleGroup:rdoBtns id:"mngr"} SwingRadioButton{text:"Genator" toggleGroup:rdoBtns id:"gntr"} ] } ]}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: SwingCheckBox{id:"isattnd" text:"Mark as Attendee"}SwingListThe 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. var list = SwingList {width:200height:50effect:DropShadow{offsetY:3 offsetX:3}items: for (i in [1..50]) SwingListItem{text:"Cloud {%5s i}"}}The code snippet above produces SwingList control as shown in figure below: Other Swing Wrapper ClassesJavaFX provides many more Swing wrapper classes to bring Swing and JavaFX closer. They include:
| The materials on this website represent a small sample of content loosely based on the book JavaFX Application Development Cookbook. The book offers far more content with over 80 recipes covering a range of topics from basics to advanced concepts. Buy the BookYou can get your copy of the book directly from the publisher. Click here to order! |




