Using Spring Boot with JavaFX

Posted at — Sep 18, 2017
Riekpil logo
Learn how to test real-world applications with the Testing Spring Boot Applications Masterclass. Comprehensive online course with 8 modules and 130+ video lessons to master well-known Java testing libraries: JUnit 5, Mockito, Testcontainers, WireMock, Awaitility, Selenium, LocalStack, Selenide, and Spring's Outstanding Test Support.

In my previous post, I showed how to use JavaFX with the Raspberry Pi 3. I promised there to follow up with a post on using Spring Boot with JavaFX, so here it goes.

Just as an example, I will be using the REST API service of the Open AQ Platform API as it is simple and does not require any authentication to get started.

Initial Setup

We will be using Maven, so we start with a minimal pom.xml that includes the latest Spring Boot version (1.5.7 at the time of writing):

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="" xmlns:xsi=""








<description>JavaFX Client for the Open AQ Platform API</description>





<relativePath/> <!-- lookup parent from repository -->






















Our minimal JavaFX application will have 1 FXML file that defines the user interface and 1 Java file to get the application running. This is main.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>

<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns="">

<Label text="Main Content"/>


And this is

package org.deblauwe.airqualityfx;

import javafx.application.Application;

import javafx.fxml.FXMLLoader;

import javafx.geometry.Rectangle2D;

import javafx.scene.Parent;

import javafx.scene.Scene;

import javafx.stage.Screen;

import javafx.stage.Stage;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.context.ConfigurableApplicationContext;


public class AirQualityFxApplication extends Application {

private ConfigurableApplicationContext context;

private Parent rootNode;


public void init() throws Exception {

SpringApplicationBuilder builder = new SpringApplicationBuilder(AirQualityFxApplication.class);

context = String[0]));

FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));


rootNode = loader.load();



public void start(Stage primaryStage) throws Exception {

Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();

double width = visualBounds.getWidth();

double height = visualBounds.getHeight();

primaryStage.setScene(new Scene(rootNode, width, height));




public void stop() throws Exception {




Some things to note:

  • The class is annotated with @SpringBootApplication to enable Spring Boot

  • We create the Spring ApplicationContext in the init() method

  • We instruct the FXMLLoader to use the context::getBean method to load controllers. This will allow us to have Spring create the controllers using component scanning and inject them into our FXML files.

Starting the application from IntelliJ IDEA shows the UI:

screen shot 2017 09 18 at 20 51 59

Injecting a Spring instantiated service

We have started our JavaFX application using Spring Boot, but there was no gain from it for that simple application. To make things more interesting, we will add a combobox that allows to select a country. For this, we adjust main.fxml:

<AnchorPane xmlns=""





<Label text="Country:"/>

<ComboBox fx:id="countriesComboBox"/>



Since we now want to have some functionality in our UI, we link the FXML file to a controller called MainController:


public class MainController {


public ComboBox<Country> countriesComboBox;


private CountryService countryService;


public void initialize() {

countriesComboBox.setConverter(new CountryNameStringConverter());



private static class CountryNameStringConverter extends StringConverter<Country> {


public String toString(Country object) {

return object.getName();



public Country fromString(String string) {

return null;




Not that much code, but quite some things are going on:

  • The class is annotated with @Component so that Spring will create a singleton out of it using component scanning

  • We can access our combo box through the countriesComboBox field (annotated with @FXML). Note that the name of the field should match with the fx:id given in the FXML file.

  • We @Autowire the CountryService, which is an interface for retrieving countries. Note how this is exactly as you autowire other dependencies in a normal Spring application.

To make this fully work, we need our Country model class (using Lombok):



public class Country {

private String code;

private String name;


The CountryService interface:

public interface CountryService {

Set<Country> getAllCountries();


And our implementation:


public class HardcodedListCountryService implements CountryService {


public Set<Country> getAllCountries() {

Set<Country> result = new HashSet<>();

result.add(new Country("AU", "Australia"));

result.add(new Country("BR", "Brazil"));

result.add(new Country("BE", "Belgium"));

return result;



Since this is our only instance of CountryService, we can just have it pick up by component scanning.

The resulting UI looks like this:

screen shot 2017 09 18 at 21 03 49


This post showed how to set up a real simple JavaFX project using Maven and Spring Boot. It also showed how to inject a Spring singleton into your JavaFX Controller. In a next post, I will show how to implement the actual call to the Open AQ Platform API.

This know-how originated during the development of a PegusApps project.

If you want to be notified in the future about new articles, as well as other interesting things I'm working on, join my mailing list!
I send emails quite infrequently, and will never share your email address with anyone else.