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
JavaFX
IntelliJ(IntelliJ IDEA)
SceneBuilder
It is assumed that the JDK and IntelliJ are already installed.
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();
}
}
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.
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
It was a shit article as announced, but for the time being, I'm glad that the JavaFX development environment was created.
Recommended Posts