Sample to create GUI application with JavaFX + IntelliJ + Scene Builder

Introduction

I wanted to make a GUI application. There are various libraries and frameworks for creating GUI applications, but personally I wish I could create it with Java, which I am most good at, so I built an environment for developing using JavaFX. ~~ I was satisfied just by making a sample! ~~

Please note that the important point is a shit article that is thrown into other articles. However, I think it's a lot of things to write the same thing as it is written in other articles, so I think it can't be helped (excuse).

The source is here. https://github.com/dhirabayashi/javafx_sample

Super-appropriate term explanation

JavaFX

IntelliJ(IntelliJ IDEA)

SceneBuilder

environment

Environment construction procedure

It is assumed that the JDK and IntelliJ are already installed.

Create a JavaFX project in IntelliJ

You may refer to the following article. It is easy to understand because it covers everything from project creation to sample code creation and execution. JavaFX application development with IntelliJ IDEA and Gradle-from environment construction to sample code-

However, it didn't work in my environment for some reason, so I changed the source a little. Class#getResource() → ClassLoader#getResource()

MainApp.java


package com.github.dhirabayashi.javafx;

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

import java.util.Objects;

public class MainApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        var cl = getClass().getClassLoader();
        Parent root = FXMLLoader.load(Objects.requireNonNull(cl.getResource("scene.fxml")));

        Scene scene = new Scene(root);
        scene.getStylesheets().add(Objects.requireNonNull(cl.getResource("styles.css")).toExternalForm());

        stage.setTitle("JavaFX sample");
        stage.setScene(scene);
        stage.show();
    }
}

Cooperation between Scene Builder and IntelliJ

Development is possible with just the above procedure, but I think it is easier to develop with Scene Builder and GUI-based than to develop entirely with code-based, so I will show you the procedure. (Although it is still a round throw)

There is official IntelliJ help on this, so check it out. Configure JavaFX Scene Builder (https://pleiades.io/help/idea/opening-fxml-files-in-javafx-scene-builder.html)

All you have to do is install Scene Builder separately and set the path in IntelliJ. If it is not in Japanese, the menu name will be different, but you can find it by opening Preferences and searching for "JavaFX".

In JavaFX, GUI is developed by editing the fxml file, but the image is that Scene Builder will rewrite the contents of the fxml in a nice way. To open the Scene Builder, select the fxml file and select "Open in Scene Builder" from the right-click menu. [^ scenebuilder] [^ scenebuilder]: You can also open the fxml file by double-clicking and select "Scene Builder" in the lower left tab, but it seems that you can not use the preview and controller skeleton code generation function described later with that procedure. , It is recommended to start from right click.

スクリーンショット 2020-08-30 11.28.22.png

This article was easy to understand how to use Scene Builder. Although it is an example of Eclipse, the operation after opening Scene Builder does not change. Basic usage of Scene Biulder

in conclusion

It was a shit article as announced, but for the time being, I'm glad that the JavaFX development environment was created.

Recommended Posts

Sample to create GUI application with JavaFX + IntelliJ + Scene Builder
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