DEV Community

Juan Sedano
Juan Sedano

Posted on • Originally published at on

Easy Java desktop UI with JavaFX and gluon scene builder

If you need some GUI for your desktop Java application you can use the scene builder from Gluon to drag and drop controls and you can then export them into a .fxml file that you can load using JavaFX. In this example we are using Java 11 and maven.

You can find the code for this example here JavaFX example.

You just need these dependencies in your project pom:

Enter fullscreen mode Exit fullscreen mode

You will also need to add this plugin:

Enter fullscreen mode Exit fullscreen mode

Where dev.jsedano.example.App is where you have your main class. Then you can load the scene like this:

package dev.jsedano.example;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class App extends Application {

    public static void main( String[] args ){

    public void start(Stage stage) throws Exception {
        File f = new File("src/main/resources/exampleui.fxml");
        Parent root = FXMLLoader.load(f.toURI().toURL());

        stage.setScene(new Scene(root));;
Enter fullscreen mode Exit fullscreen mode

In this example our fxml file is called exampleui.fxml

That’s pretty much it.

But how about actually creating the fxml file, download and install Gluon scene builder. In this example we will start with a Basic Application:

basic application

We will drag a button and a label. Select the button and we will change the text to Press me! on the Properties tab and in the Code tab we will set On Action to buttonWasPressed:

adding action to button

For the label we will delete the text and set the fx:id to myLabel.

adding id to label

Now we need to define the controller. On the left panel look for Document and then Controller and set the Controller class to what your controller will be named, on this example we will use dev.jsedano.example.UIController (this is the fully qualified class name).

defining the controller

And then we can save the generated fxml directly into our project resources folder, in this example we save to src/main/resources/exampleui.fxml

Now create the controller on the project

package dev.jsedano.example;

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class UIController {

    private Label myLabel;

    private void buttonWasPressed(){
        myLabel.setText("button was pressed");
Enter fullscreen mode Exit fullscreen mode

We run the project using mvn clean javafx:run

And if everything was correct we should be seeing this when we press the button:

button was pressed

From then on you can create anything you need, for documentation on JavaFX visit Download the complete code from this example here JavaFX example.

Top comments (1)

fntp profile image

As a matter of fact, I used to use SWT all the time, but then I switched to JavaFX and was obsessed with building the fxml of the page.