JavaFxFXML Tutorial
JavaFxFXML Tutorial
Andreas has graduated from Computer Science and Bioinformatics at the University of Linz. During his studies he
has been involved with a large number of research projects ranging from software engineering to data engineering
and at least web engineering. His scientific focus includes the areas of software engineering, data engineering, web
engineering and project management. He currently works as a software engineer in the IT sector where he is mainly
involved with projects based on Java, Databases and Web Technologies.
JavaFX FXML Tutorial
Posted by: Andreas Pomarolli in FXML April 14th, 2016 1 Comment 2064 Views
This is a JavaFX FXML Tutorial. In this tutorial we will discuss how to use FXML for
creating the GUI of an application. The first three chapters are also part of the
article JavaFX FXML Controller Example. Given the fact, that this article represents a
tutorial, it contains also the Controller Example.
FXML is an XML-based language designed to build the user interface for JavaFX
applications. You can use FXML to build an entire Scene or part of a Scene . FXML allows
application developers to separate the logic for building the UI from the business logic. If
the UI part of the application changes, you do not need to recompile the JavaFX code.
Instead you can change the FXML using a text editor and rerun the application. You still
use JavaFX to write business logic using the Java language. An FXML document is an
XML document.
A JavaFX scene graph is a hierarchical structure of Java objects. XML format is well suited
for storing information representing some kind of hierarchy. Therefore, using FXML to
store the scene-graph is very intuitive. It is common to use FXML to build a scene graph in
a JavaFX application.
Want to master JavaFX?
Subscribe to our newsletter and download the JavaFX
Programming Cookbook right now!
In order to get you prepared for your JavaFX development needs, we have compiled numerous
recipes to help you kick-start your projects. Besides reading them online you may download
the eBook in PDF format!
Email address:
Sign up
Table Of Contents
1. Introduction to FXML
1.1 The FXML Code
1.2 Adding UI Elements
1.3 Importing Java Types in FXML
1.4 Setting Properties in FXML
1.5 Specifying FXML Namespace
1.6 Assigning an Identifier to an Object
1.7 The Corresponding Java Class
1.8 The GUI
2. Using Script Event Handlers
2.1 The FXML Code
2.2 The Corresponding Java Class
2.3 The GUI
3. Using Controller Event Handlers
3.1 The FXML Code
3.2 The Controller Class
3.3 The Corresponding Java Class
3.4 The GUI
4. Including FXML Files
4.1 The FXML Code
4.2 The Corresponding Java Class
4.3 The GUI
5. Reusable Objects and Referencing Another Element
5.1 The FXML Code
5.2 Creating Reusable Objects in FXML
5.3 Referencing Another Element
5.4 The Corresponding Java Class
5.5 The GUI
6. Using Constants
6.1 The FXML Code
6.2 The Corresponding Java Class
6.3 The GUI
7. Binding Properties
7.1 The FXML Code
7.2 The Corresponding Java Class
7.3 The GUI
8. Using Resource Bundles
8.1 The FXML Code
8.2 The Properties Files for the Resource Bundles
8.2 The Corresponding Java Class
8.3 The GUI
9. Download Java Source Code
7. Binding Properties
7.1 The FXML Code
FxFXMLExample7.fxml
01 <?xml version="1.0" encoding="UTF-8"?>
02
03 <?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
04
05 <VBox fx:id="vbox" layoutX="10.0" layoutY="10.0" prefHeight="250.0" prefWidth="300.0" sp
06 <style>
07 -fx-padding: 10;
08 -fx-border-style: solid inside;
-fx-border-width: 2;
09 -fx-border-insets: 5;
10 -fx-border-radius: 5;
11 -fx-border-color: blue;
12 </style>
13 <children>
<fx:include source="/FXFXML/FxFXMLLabel.fxml" fx:id="inputLbl" text="Please insert
14
15
16 <TextField fx:id="inputText" prefWidth="100.0" />
17 <fx:include source="/FXFXML/FxFXMLLabel.fxml" fx:id="outputLbl" text="Your Input:"/
18 <TextArea fx:id="outputText" text="${inputText.text}" prefHeight="100.0" prefWidth=
19 </children>
</VBox>
20
21
FXML supports simple property bindings. You need to use an attribute for the property to
bind it to the property of another element or a document variable. The attribute value starts
with a $ symbol, which is followed with a pair of curly braces.
The following FXML content creates a VBox with two TextFields. The text property of the
outputText field is bound to the text property of the inputText field.
1 <TextArea fx:id="outputText" text="${inputText.text}" prefHeight="100.0" prefWidth="10