JavaFX modules are no more part of OpenJDK. From version 11 JavaFX modules are separated from Java SDK and it’s a major version released after 8. JavaFX have its own website now https://openjfx.io/. In this article we will go through the process of creating and running simple JavaFX window using Maven. JavaFX 11 runtime is available in maven repository so we will use it with maven.
To make it simple I will use a terminal and a text editor to create this simple JavaFX window.
In the terminal execute below command to generate an empty maven project.
This command will generate a maven project with org.hello.fx package. Update pom.xml with below content.
We need org.openjfx dependency, this dependency is the runtime environment for JavaFX so I added it and also we need below two plugins;
maven-compiler-plugin: To compile the project
exec-maven-plugin: I provided org.hello.fx.HelloFX as main class in the configuration. I will create this class in the next section, this class will initialize and run’s JavaFX class.
Create JavaFX class
Create a new class in hello-fx/src/main/java/org/hello/fx/HelloFX.java
This class opens a simple window with ‘Say Hello World’ button in center. Clicking on this button will print “Hello World!” in the console.
Run Application
Execute below command in the terminal to compile and run HelloFX class.
Maven will compile and run’s HelloFX class. New window will open with the button in center. Click on this button to see ‘Hello World!’ in the console.
Conclusion
We created and executed a simple JavaFX 11 window. You can import this project to any Java IDE.