JavaFx + Spring Boot + Gradle: Project Setup guide and test

JavaFx + Spring Boot + Gradle: Project Setup guide and test

I am pleased to introduce you to three actors of today’s performance. Each of them is a master in his field, but together they create something very powerful. So there are of course JavaFX, Spring Boot and Gradle. See what the project configuration looks like from scratch.

  1. What will you learn from this guide
  2. Creating a Gradle/Java project using IntelliJ IDEA
  3. Configuring Spring Boot & Gradle application
  4. Integration of JavaFX with Spring Boot application
  5. Testing the described solution


Application sources

You can clone the current version of the template described in this guide from my gitlab repository:


1. What will you learn from this guide

Recently, I was faced a project to write a desktop Java application. It was quite a natural choice to use JavaFX as the main framework for the GUI, the more that it overpowered such legends as SWING or AWT. Currently, JavaFX is also recommended by Oracle as the main Java framework for programming GUI for desktops.

When I started implementing the requirements, I realized that I missed what I knew from the Spring MVC. I mean the IOC and the huge amount of solutions that Spring has implemented like externalized configuration, profiles, logging, servlets and much more. As soon as this idea was born in my head, I already knew that I wanted Spring in my project. I did what I thought and I’m describing it

The last of the three is Gradle, which is one of the most powerful tools for building projects. For the uninitiated – it’s Maven, just on steroids. Gradle is incredibly flexible with an extremely simple adaptability to any project (practically in any language, not only in Java).

In this guide, I describe the entire process of configuring the title project, starting from the collection of programs and tools, by integrating Spring Boot with JavaFX, up to the final application tests.


2. Creating a Gradle/Java project using IntelliJ IDEA

For the purpose of this tutorial I will use JDK8 and IntelliJ IDEA as IDE. When starting to create a project, it is worth to prepare the following tools and libraries:

Once we have collected the tools, we can proceed to the creation of the project itself. The first choice we will face is a tool for building a project and managing library dependencies. Maven or Gradle – for the purposes of this guide I chose the second option. So let’s start creating the project.

Select Gradle on the new project screen and check Java in the additional libraries panel. Also remember to choose the appropriate JDK.


Then write the metadata you probably already know from the Maven projects – a group, artefact name and version of your application.


At the end configure Gradle. The most important thing is to select Use auto-import and Use default gradle wrapper options. Also make sure that Gradle JVM is set to the right JDK directory.


That would be all about the configuration of the Gradle project – you can go to the next step.


3. Configuring Spring Boot & Gradle application

Spring is an undisputed champion in the category of IOC frameworks, which is why I decided to use its advantages and amazing possibilities in my project. Integration of Spring Boot with Gradle is described in detail in the following post.


Based on the mentioned tutorial, modify the file build.gradle like below and create class for testing purposes.



After launching the application, you should see the following result – a message Hello World and 0 as application return code.


4. Integration of JavaFX with Spring Boot application

Start from creating the sample.fxml form in the resources/fxml directory. The form consists of a panel and a WebView Component inside of it. I will use it later in order to check integration with Spring Boot.


The next step is to modify the class, which was originally just a Spring Boot application, to the JavaFX application under Spring’s control. To do this, modify it so that it inherits from the javafx.application.Application class.


Next, prepare a few class fields that you will use in the methods implemented in the next steps.


What is changed above all is the method of launching of the application. There is a code which starts spring context in the main method. Change it to invocation of launch method which starts JavaFx application. The start of Sring context you will implement in the next step.


We smoothly go to the implementation of the most important method from the point of view of JavaFX integration with Spring Boot, called init. The code inside starts Spring context, creates FXMLLoader and ceding the control of beans creation to the Spring’s bean factory.


The start method is basically a standard code found in a pure JavaFX application. It consists of loading and configuring the form and displaying the scene.


The last element from the integration point of view is to stop Spring when the JavaFx application is stopping. You achieve this effect by overwriting the stop method by the following code.


For readability of code, I decided to delete CommandLineRunner. Below is the final version of the application code, which is already a fully functional hybrid of JavaFx and Spring Boot.


5. Testing the described solution

The most classic case, which is also well suited to the test of the described integration, is in my opinion the creation of a bean which is used as form controller for sample.fxml and it is delivered by Spring’s bean factory. The task of the mentioned controller will be to load the page of my blog in the WebView component. The website address will be provided from the file.

Start with preparing the file with the following content:


Then create a controller and plug it in the sample.fxml form.


It is worth paying attention to the line 13 of the controller, where we inject the property value into the controller field, and lines 16-19, in which we use them to load a web page in the WebView component.


The controller must annotated by @Controller so that Spring can scan and create it. Otherwise, we’ll get an exception:


At this point, our application is ready for the final test. If you followed this guide, then the structure of the project should be analogous to the following.


There is nothing else left for us to run our application and keep our fingers crossed to get the following result.


If you think this post is valuable, please leave me +1 or just share it. This action will allow me to reach a wider audience. Thank You in advance.


Leave a Reply

Notify of
Close Menu