In this lab, we are going to use FXML to create a JavaFX application, FXML will help us a lot in separating the design and the application logic, and it will make it easier for us to design and change the design of our program.
FXML is a user interface markup language, the language itself doesn't have any logic, but it's just a description of the user interface.
Notice that at the start of our FXML file, we describe what we need to import, and then the XML describes each component and its properties.
Note: We won't write FXML from scratch.
You could load your design using FXMLLoader
class:
After creating your FXML design, we will need to connect it to some code, this code will handle events, and change other things in the view while your program is running.
FXML allow us to specify a controller to the FXML file, the controller is just a Java class:
Notice that you will need @FXML
for everything that communicates with your FXML file.
You could assign an ID to a component, which will link the component to a variable inside the controller.
Notice that Label
has an ID label
.
In your controller, you should define a data field named label
Scene Builder is a GUI application to build our FXML application, you could drag and drop components in it to create your FXML file, notice that you should understand the JavaFX before using the drag and drop application to avoid a lot of mistakes.
Scene Builder Main Window
You can download Scene Builder for free from its original site: https://gluonhq.com/products/scene-builder/
You could use your prefered IDE and link it with scene builder to create a smoother development process, the scene builder will auto-complete a lot of things using this method.
In NetBeans for example, you will need to go to Tools
-> Options
-> Java
-> JavaFX
to link Scene builder with it.
NetBeans JavaFX Options Window
In this section, we will explain how to create an exe file to run your Java application, the first thing we will need is to create a jar file.
if you created a maven project, you can export a jar file with the main class to run by editing the pom.xml
file, just add the following plugin to your build options:
after building your project, you will find a jar
file in the target
folder inside your project, you could run this using the following command:
Now to build our exe file, we will use a program called launch4j, which will help us bundle our jar, and search for the correct JRE
.
Launch4j main window
Notice that you will have to select your jar file and the output file, and from the JRE tab you will have to specify your minimum JRE version.
You could modify other properties, for example, you can add an icon to your exe file.
TADA Click build and you will have an exe file.
Note that you could bundle the JRE with your application (So the user won't have to install JRE).
Your first exe Application
In this task, you should create a simple web browser, you should build your application as an FXML JavaFX application.
You will get the full mark if your browser could do the following:
You will get a bonus if you implemented the following:
You could search on google by going to the following link
And here is a gif of my solution.
Task 1 Expceted Output
Programming
Java
IUG