JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~

0. Introduction

Now, the author is under pressure to create software that uses the acclaimed GUI tools. ~~ I left it for a long time ~~ I started it recently, but it took a lot of time to build an unexpected environment, so I will write it here as a memorandum.

1. What is JavaFX?

Is there anyone who thinks "GUI in Java is AWT! Swing!"? Actually, I heard that the hurdle for GUI tool development has been lowered considerably recently with the emergence of a framework called JavaFX (I just heard).

I happened to find it when I was wandering around the net, "Ah, GUI? How do I make it?" Today, I would like to introduce a few entrances to JavaFX that are so convenient.

2. Writer's environment

3. Environment construction

3.1. OpenJDK installation

3.1.1. Download

Download the version of JDK you want to use from OpenJDK Site. However, note that JavaFX requires JDK 11 or higher.

■ The above site 191121-0001.png In the middle of this image, download it from the tar.gz link to the right of macOS / x64. I used 13.0.1.

3.1.2. Installation

Extract the downloaded tar.gz file and change to the following directory. /Library/Java/JavaVirtualMachines

/
└── Library
    └── Java
        └── JavaVirtualMachines
            └── jdk-13.0.1.jdk
                └── Contents
                    └── ...

Execute the following command as confirmation.

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    13.0.1, x86_64:	"OpenJDK 13.0.1"	/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
    11.0.2, x86_64:	"OpenJDK 11.0.2"	/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home

The execution results do not have to be exactly the same. Make sure the version you just installed is recognized.

3.2. Project setup

3.2.1. Creating a project

Launch IntelliJ IDEA and create a new project from Create New Project. This time we will use JavaFX with Gradle, so of course let's create a Gradle project.

■ Project creation screen 191121-0002.png Please set the Project SDK to 13.

Specify GroupId and ArtifactId on the next screen.

The next screen is where you set up your project, but I basically don't mess with it. All you have to do is make sure your Gradle JVM is 13.

After specifying the folder to be used on the next screen, the project will be created. Let's wait for a while until the various processes are completed.

3.2.2.build.gradle settings

** Add ** the following part to build.gradle. I haven't edited anything that isn't written.

build.gradle


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

dependencies {
    //This runtimeOnly group is for making cross-platform jars.
    //You can reduce the size of the jar file by deleting unnecessary ones.
    runtimeOnly "org.openjfx:javafx-base:$javafx.version:win"
    runtimeOnly "org.openjfx:javafx-base:$javafx.version:linux"
    runtimeOnly "org.openjfx:javafx-base:$javafx.version:mac"
    runtimeOnly "org.openjfx:javafx-controls:$javafx.version:win"
    runtimeOnly "org.openjfx:javafx-controls:$javafx.version:linux"
    runtimeOnly "org.openjfx:javafx-controls:$javafx.version:mac"
    runtimeOnly "org.openjfx:javafx-fxml:$javafx.version:win"
    runtimeOnly "org.openjfx:javafx-fxml:$javafx.version:linux"
    runtimeOnly "org.openjfx:javafx-fxml:$javafx.version:mac"
    runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:win"
    runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:linux"
    runtimeOnly "org.openjfx:javafx-graphics:$javafx.version:mac"
}

javafx {
    version = "13"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = 'maru.test.Launcher'
jar {
    manifest {
        attributes 'Main-Class': 'maru.test.Launcher'
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

■ Now it looks like this (the lower part is a little cut) 191122-0003.png

3.2.3. Implementation of sample code

The sample code was borrowed from OpenJFX Official.

src/main/java/maru/test/Launcher.java


package maru.test;

import javafx.application.Application;

public class Launcher {
    public static void main(String... args){
        Application.launch(MainApp.class);
    }
}

src/main/java/maru/test/MainApp.java


package maru.test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class MainApp extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("scene.fxml"));
        
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
        
        stage.setTitle("JavaFX and Gradle");
        stage.setScene(scene);
        stage.show();
    }
}

src/main/java/maru/test/FXMLController.java


package maru.test;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

import java.net.URL;
import java.util.ResourceBundle;

public class FXMLController implements Initializable {
    
    @FXML
    private Label label;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        label.setText("Hello, JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + ".");
    }
}

src/main/resources/maru/test/scene.fxml


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

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


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="maru.test.FXMLController">
    <children>
        <Label fx:id="label" text="Label" />
    </children>
</StackPane>

src/main/resources/maru/test/styles.css


.label {
    -fx-text-fill: blue;
}

■ Like this 191122-0004.png

3.2.4. Compile and run

Click Gradle in the upper right corner and run Tasks> build> build. Once that's done, run Tasks> application> run a little above.

■ This is 191122-0005.png If you see something like this, you're successful!

■ "Like this" 191121-0006.png I think you have a jar file in build / libs /. ~~ This jar file is a fat jar (a jar file that also contains libraries), so it should work on anyone's PC. ~~ ** I'm sorry I probably lied. If you want fat Jar, add shadow. ** **

3.2.5. Where the description changes depending on the folder structure

Please change the place surrounded by <> to your liking. Only the parts that depend on the folder structure are extracted.

file name
build.gradle 2 places
Launcher.java One place
MainApp.java 2 places
scene.fxml One place

build.gradle


mainClassName = '<Classes for launchers that do not inherit from the Application class>'
jar {
    manifest {
        attributes 'Main-Class': '<Same as mainClasName above>'
    }
}

Launcher.java


public static void main(String... args){
    Application.launch(<A class that inherits from the Application class>.class);
}

MainApp.java


@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("<fxml file>"));
    scene.getStylesheets().add(getClass().getResource("<css file>").toExternalForm());
}

scene.fxml


<StackPane (Omission) fx:controller="<Controller class>">

Probably over. I'm sorry if there is an oversight. .. ..

4. Finally

how was it? Wasn't it easier than I expected?

By the way, SceneBuilder is convenient for building screens. You can also do this by setting IntelliJ IDEA.

■ Such a thing 191122-0001.png I think this has lowered the hurdles for GUI application development. Enjoy a good GUI application life!

5. References

Recommended Posts

JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~
[Jakarta EE 8 application development with Gradle] 1. Environment construction
Java web application development environment construction with VS Code (struts2)
PostgreSQL environment construction with Docker (from setup to just before development)
Introduction to Java development environment & Spring Boot application created with VS Code
Development environment construction using IntelliJ IDEA + Maven + Tomcat 9
How to build docker environment with Gradle for intelliJ
Introduce JavaFX 15 and do GUI development with VS Code
How to build Java development environment with VS Code
[Environment construction] Build a Java development environment with VS Code!
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Create a Java and JavaScript team development environment (gradle environment construction)
Environment construction for Servlet application development
Notes on building Kotlin development environment and migrating from Java to Kotlin
Spring5 MVC Web application development with Visual Studio Code Environment construction (Installation of JDK11, Maven, Tomcat, Visual Studio Code)
Minecraft Mod development environment construction (IntelliJ IDEA + Minecraft Forge 1.15.2) + Hello World Mod creation
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
I tried to build a Firebase application development environment with Docker in 2020
Java EE 8 (using NetBeans IDE 8.2) starting from sample code Part 1 Environment construction
Prepare Java development environment with VS Code
Laravel development environment construction with Docker (Mac)
Environment construction summary with rvm and postgresql
[For beginners] Laravel Docker AWS (EC2) How to easily deploy Web application (PHP) from 0 (free) ②-Docker development environment construction-
How to output jar with main class specified by gradle in Intellij IDEA
Try modding with Minecraft Forge 1.15.1 ① [Building development environment] [Multiple versions supported] [IntelliJ IDEA]
From creating a Spring Boot project to running an application with VS Code
Introduction to Robot Battle with Robocode (Environment Construction)
Java GUI sample
A memorandum when trying to create a GUI using JavaFX
Serial communication sample using jSerialComm
Serial communication sample using purejavacomm
Sample to create GUI application with JavaFX + IntelliJ + Scene Builder
Sample code using Minio from Java
Sample code for Singleton implementation using enum
Rails development environment created with VSCode and devcontainer
A reminder of Docker and development environment construction
[Jakarta EE 8 application development with Gradle] 2. Project creation
Wordpress local environment construction & development procedure with Docker
Build a Java development environment with VS Code
JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~
Java GUI sample
A memorandum when trying to create a GUI using JavaFX
Serial communication sample using jSerialComm
Serial communication sample using purejavacomm
Sample to create GUI application with JavaFX + IntelliJ + Scene Builder
Sample code using Minio from Java
Sample code for Singleton implementation using enum
A memorandum when trying to create a GUI using JavaFX
How to create an application
[Note] Scene transition with JavaFX
Preparing to create a Rails application
Easy way to create an original logo for your application (easy with your smartphone)
Create a simple web application with Dropwizard
Create a GUI JSON Viewer with Ruby/GTK3
Sample to create custom tag for JSP
Sample to start Ubuntu with Deployment with client-go
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
Build Java development environment with VS Code on Mac
Introduction to Slay the Spire Mod Development (2) Development Environment Construction
BEAR application Docker development environment construction example (docker-sync, Mutagen)
Stable development environment construction manual for "Rails6" with "Docker-compose"
Build Java development environment with WSL2 Docker VS Code
Prepare the environment for java11 and javaFx with Ubuntu 18.4
How to install Gradle and Kotlin with SDKMAN (Mac)
Build Java program development environment with Visual Studio Code
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
[IntelliJ IDEA] How to display code completion candidates without distinguishing between uppercase and lowercase letters
I was addicted to WSl when trying to build an android application development environment with Vue.js