JavaFX Tutorial: Getting Started

Hello, Habr! I present to you the translation of the article “JavaFX Tutorial: Getting started” by Vojtech Ruzicka.



How to set up and get started with JavaFX using the Maven, Gradle or JavaFX SDK.



All posts in the JavaFX series:



  1. JavaFX Tutorial: Getting Started
  2. JavaFX Tutorial: Hello World!
  3. JavaFX Tutorial: FXML and SceneBuilder
  4. JavaFX Tutorial: Basic Layouts
  5. JavaFX Tutorial: Advanced Layouts
  6. JavaFX Tutorial: CSS styling
  7. JavaFX Weaver: Integrating JavaFX and Spring Boot Applications


Java installation



JavaFX, of course, requires that you have JDK installed. Getting the required dependencies varies for different versions of Java.



JavaFX was introduced as part of the release of Java 8. However, it was later removed from the JDK and

ported to a separate module in Java 11.



This means that if you are targeting Java 8-10, you have all the necessary dependencies as part of your JDK already. Hooray! If you are using a newer version of Java - that is 11+, you need to get the dependencies separately.



Maven



Getting and managing dependencies manually is rather inconvenient, and in a real application you would rarely do that. It is much better to use a dependency management system such as Maven or Gradle. That way, you can simply declare which dependencies you are using and which versions, and the system will take care of the rest.



Archetype Maven



Of course, you can set up your Maven project manually, from scratch. However, you may prefer a more convenient way to create the structure and content of the base project for you using Maven.



Maven has the concept of archetypes, which essentially means that you can create different types of projects from a template. There are numerous archetypes for various types of projects, and fortunately, there are a couple for JavaFX. The archetype you can choose depends on which version of Java you are using.



You can read more about Maven archetypes in the following post: Maven archetypes tutorial .



Java 8 Archetype



You can use com.zenjava: javafx-basic-archetype , or you can find other archetypes on your own if this doesn't suit you.



You can easily generate a project from the command line using Maven using the archetype above:



mvn archetype:generate -DarchetypeGroupId=com.zenjava -DarchetypeArtifactId=javafx-basic-archetype
      
      





Alternatively, you can create a new Maven project from an archetype right in your IDE.



Java 11 Archetype



For Java 11, you can use org.openjfx: javafx-archetype-simple .



To create a project, simply run:



 mvn archetype:generate -DarchetypeGroupId=org.openjfx -DarchetypeArtifactId=javafx-archetype-simple
      
      





Manual Maven Tuning



If you want more control, you can manually configure your Maven project without generating it from the archetype.



First you need two components. The first is Maven Plugin for Java FX .



Just add the following to your pom.xml:



 <build> <plugins> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>0.0.3</version> <configuration> <mainClass>com.example.App</mainClass> </configuration> </plugin> </plugins> </build>
      
      





note that
 <mainclass>
      
      



should point to your main class, which has a main method and extends javafx.application.Application. We will talk about this in the next article in the series.



The second part adds a dependency for JavaFX controls :



 <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>11.0.2</version> </dependency>
      
      





Gradle



Gradle currently does not support creating projects directly from archetypes .



You can use the unofficial Gradle Archetype plugin and use the Maven archetypes mentioned above.

Alternatively, you can create your project using Maven from an archetype, and then convert it to a Gradle project using the following command in the directory containing your pom.xml:



 gradle init
      
      







Manual Gradle



As with manual Maven setup, you need to add the JavaFX plugin:



 plugins { id 'application' id 'org.openjfx.javafxplugin' version '0.0.8' }
      
      





And the dependency for the controls:



 javafx { version = "11.0.2" modules = [ 'javafx.controls' ] }
      
      





JavaFX SDK



There is another possibility to use JavaFX locally. You can download the JavaFX SDK. It contains all the necessary libraries, which you can then link to the project in your IDE or add to the classpath.



This can be useful when you are not familiar with Gradle or Maven and just for local development.



When distributing your application, this becomes inconvenient, since you need to make sure that you include all the necessary dependencies.



With this option, you can generate a project without Maven / Gradle in your IDE, which contains all the necessary files. In IntelliJ IDEA, you can simply go to:



 File → New → Project → JavaFX
      
      





Additional reading



Hello World JavaFX application source code - Gradle

Hello World JavaFX application source code - Maven

JavaFX 13 and IntelliJ

JavaFX 13 and NetBeans

JavaFX 13 and Eclipse



What's next



This article is the first in a series of JavaFX. In the next, we’ll show you how to create and run your very first JavaFX application .



All Articles