Using Spring Boot with JavaFX

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.png

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.png


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.


11 thoughts on “Using Spring Boot with JavaFX

  1. Pingback: Using JavaFX on Raspberry Pi 3 with Gluon | Wim Deblauwe's Blog

  2. Pingback: Using Spring Boot with JavaFX – Using Spring Profiles | Wim Deblauwe's Blog

  3. Pingback: JavaFX links of the week, September 25 | JavaFX News, Demos and Insight // FX Experience

  4. Pingback: Java desktop links of the week, September 25 – Jonathan Giles

  5. Pingback: fxexperience.com新闻(2017-09-25) | JavaFX中文、OSGi、Eclipse开源资料

  6. Thanks for the example.
    I was trying to do something similar with SWT, but it’s a pain, really. I’ll just go with JavaFX.

  7. Old article, but still relevant for people who want to combine FX and Spring Boot. I noticed in my setup (Spring Boot 2.1.3 & JDK 8) I had to define the base packages. Otherwise Spring couldn’t find my controller (NoSuchBeanDefinitionException). So, on your Main application class; add @ComponentScan(basePackages = “”).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s