This is a basic project on Java digned applying JavaFX JavaFX and IntelliJ IDEA by JetBrains File -> New -> Project... -> JavaFX ). JavaFX is a toolkit GUI (Graphical user interface) for Java. Another words, JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms. This app represents a simple form with a filter, which displays the given data from the database. The 'database table' looks like this:
Column name | Datatype | PRIMARY KEY | NOT NULL | UNIQUE KEY | AUTO_INCREMENT |
---|---|---|---|---|---|
id_employee | int | ✅ | ✅ | ✅ | ✅ |
personnel_number | int | ✅ | ✅ | ||
name | varchar(45) | ✅ | |||
surname | varchar(45) | ✅ |
The filter allows you to display the results of a SQL query and retrieve an employee list based on the text request and the item (id, personnel number, name, surname) you select from a drop-down list. The "find" button launches the filtering process.
Each application created with JavaFX consists of a hierarchy of some basic components:
- stages;
- scenes
- nodes.
Stage basically represents a window. Application can have multiple stages, but needs at least one. A scene represents the contents of a stage. Each stage can have multiple scenes, which you can switch. Each scene can contain various components, which are called nodes. These can be controls like buttons or labels or even layouts, which can contain multiple nested components. Each scene can have one nested node, but it can be a layout, which can host multiple components. The nesting can be multiple levels deep - layouts can contain other layouts and regular components.
Simply put, each application can have multiple stages - windows. Each stage can switch multiple scenes. Scenes contain nodes.
Each JavaFX application needs to have a main application class. That is - a class that extends javafx.application.Application
.
You also need to override the abstract method from the Application class - public void start(Stage primaryStage) throws Exception
.
Inside the main()
method, we can launch our application using Application.launch()
.
In order to show a window we need to load stage, which we have as an input parameter in the start method. The same way we
can load another components of windows and multiple icons.
Finally, we can easily show it using primaryStage.show()
.
Main class look something like this:
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(EmployeeApplication.class.getResource("employeeSort.fxml"));
Path path = Paths.get("src/main/resources/icons/bussiness-man.png");
InputStream inputStream = new FileInputStream(path.toFile());
Image image = new Image(inputStream);
Scene scene = new Scene(fxmlLoader.load(), 600, 400);
stage.setScene(scene);
stage.setTitle("Employee app");
stage.getIcons().add(image);
stage.show();
}
in addition to the procedural construction of your UI, we can use declarative XML markup. It turns out XML's hierarchical structure is a great way to describe a hierarchy of components in the user interface. This XML format specific to JavaFX is called FXML. Here you can define all your components and their properties and link it with a Controller, which is responsible for managing interactions.
fx:controller="by.javafx.petrovich.demo.controller.ShowEmployeesController"
When loading basicFXML.fxml, the loader will find the name of the controller class, as specified in the FXML
by
fx:controller=" org.scenebuilder.BasicFXMLController"
Then the loader will create an instance of that class, in which it will try to inject all the objects that have an fx:id
tag in the FXML and are marked with the @FXML
annotation in the controller class.
In this sample, the FXMLLoader will create the label based on <Label ... fx:id="label"/>
, and it will inject the label
instance into the @FXML
field.
private Label label;
The FXMLLoader also parses all the custom event handlers included, like "#onFindButtonClick"
, and it will look for the
matching methods in the controller that register those action handlers. In my case, @FXML
protected void onFindButtonClick()
Finally, when the whole FXML has been loaded, the FXMLLoader will call the controller's initialize method.
While the FXML file can be edited within the IDE, it is not recommended, as the IDE provides just basic syntax checking and autocompletion, but not visual guidance. The best approach is opening the
FXML
file withScene Builder
, where all the changes will be saved to the file.
Tech Stack
Clone the project
git clone https://github.com/Petrovich-A/JavaFX.git
Optionally you can download the .jar
file by following the link
and populate the database with data by running the sql query.
I appreciate your assistance in a code review n discussing some issues I encountered with. Thank you, buddy. 😎
Made with contrib.rocks.