๐ JavaFX Tutorial: First FXML Application
๐ฏ Goal
In this tutorial, you will learn how to build a simple JavaFX application using:
ApplicationclassFXMLfor UI designControllerfor logic
๐ Project Structure
org.example.testingtoturials
โ
โโโ Launcher.java
โโโ HelloApplication.java
โโโ HelloController.java
โโโ hello-view.fxml
๐ 1. Launcher Class
This class is the entry point of the application.
package org.example.testingtoturials;
import javafx.application.Application;
public class Launcher {
public static void main(String[] args) {
Application.launch(HelloApplication.class, args);
}
}
๐ก Explanation
- We use
Application.launch()to start the JavaFX application. - It calls the
start()method insideHelloApplication.
๐ฅ๏ธ 2. Main Application Class
This class loads the UI and displays the window.
package org.example.testingtoturials;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(
HelloApplication.class.getResource("hello-view.fxml")
);
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
}
๐ก Explanation
FXMLLoaderloads the UI from the.fxmlfile.Scenerepresents the content inside the window.Stageis the main window.stage.show()displays the application.
๐ฎ 3. Controller Class
Handles user interactions (logic).
package org.example.testingtoturials;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
@FXML
private Label welcomeText;
@FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}
๐ก Explanation
@FXMLconnects Java code with FXML elements.welcomeTextis linked to a Label in the UI.- When the button is clicked, the label text changes.
๐จ 4. FXML UI File
Defines the user interface.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="20.0"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.example.testingtoturials.HelloController">
<padding>
<Insets top="20.0" right="20.0" bottom="20.0" left="20.0"/>
</padding>
<Label fx:id="welcomeText"/>
<Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox>
๐ก Explanation
VBoxarranges elements vertically.fx:controllerlinks this file toHelloController.fx:id="welcomeText"connects the Label to Java code.onAction="#onHelloButtonClick"calls the method in the controller.
๐ How Everything Works Together
Launcher.main()starts the appHelloApplication.start()loads the FXML- FXML creates UI and links to
HelloController - Button click โ calls
onHelloButtonClick() - Label text updates ๐
๐งช Final Result
When you run the app:
- A window appears with a button
- Clicking the button updates the text
โ ๏ธ Common Mistakes
- โ Wrong FXML path โ
getResource("hello-view.fxml") - โ Missing
@FXMLannotation - โ fx:id not matching variable name
- โ Method name mismatch in
onAction