Tool creation with JavaFX & self-contained package (JavaFX edition)

About this page

When you want a tool in a project, Excel macros are still on the business trip in the SI area, It's heavy and difficult to maintain, and I wonder if there is anything else. I will try it as a Java client application and summarize the procedure.


What is JavaFX?

One of the things that will be taken care of in the introduction to Java is AWT and Swing, and JavaFX is the GUI library that appeared as the successor to them. By adopting design tools such as SceneBuilder, XML and CSS, the aim is to separate design and business logic.


What is a self-contained package?

The bottleneck of Java client applications is that they require a Java execution environment for the device to be started. Then, it works or does not work due to the version difference for each terminal. This is not popular. Therefore, JRE is also installed in the distribution application so that the application can refer to it. Now you can run the app regardless of the environment on the device side. This will be popular.


Step 1. Environment

This time, Windows10 and eclipse are used as the development environment.


1. 1. Add javaFX project plugin to eclipse

Help ⇒ From the Eclipse marketplace, enter JavaFX etc. to search. Let's install a plugin called e (fx) clipse. (If it does not appear in the search, the version of Eclipse may not be supported. Please upgrade or get the latest version of Eclipse to solve the problem. ) 記事用.png


2. Install Scene Builder

Scene Builder is a design tool that allows you to build GUI parts such as text boxes and buttons by operating the mouse. Looking at Introduction to Java in Tohoho, I remembered the time when I was building steadily, and it became convenient without my knowledge. I just think.

The tool can be downloaded from this site. After installation, let's link the Scene Builder executable file to eclipse. image.png


3. 3. Create a JavaFX project

Select JavaFX Project from File → New → Other Java project settings are your choice. (I think the initial settings are fine for the time being) When you proceed to the next step, you will finally be asked for UI settings. In this article, set the following.

a.png


Important: When creating with Java 11 or later

Since Java11, OpenJDK and OpenJFK have been separated. The JavaFX module does not exist in the above procedure, so you need to devise one more thing.

This time, it was solved by merging the OpenJFX module into OpenJDK. I am creating a merged version of the JDK by borrowing the procedure / tool from the following.

Being separated from the JDK makes JavaFX more difficult to work with. It is quite complicated to put OpenJFX in the PATH and specify --add-modules javafx.controls as the VM argument when executing the application. This complexity is due to JavaFX being separated from the JDK. Once you merge JavaFX into the JDK, you can use JavaFX like traditional Java without worrying about module specifications.

Add the created JDK to eclipse and you're ready to go! image.png

Reference: There seems to be another way to add the OpenJFX library to the dependency from Maven Central.


Step 2. Build a view

You can move to Scene Builder by right-clicking Form.fxml ⇒ "Open with Scene Builder". びるだ.png

The Document at the bottom left corresponds to the frame of the illustration. At first, only a Container called AncorPane is declared. First of all, let's secure the area of this Pane.


Edit frame settings

Click Ancor Pane at the bottom left ⇒ Enter "300" in the leftmost cell of Layout "Padding" on the right. If you click the ">" to the right of it, it will be copied to other cells as well. I think a white background has appeared in the center. This is the area of AncorPane. image.png


Place other parts

I will place buttons and so on on this. Drag and drop Controls ⇒ TextField on the left side to the center to add TextField to Document. Enter the ID in Code ⇒ fx: id on the right side and set the ID of this TextField.

Buttons are also arranged in the same way. Set the ID in fx: id and On Action. Now you are ready to "type in the text and press the button". ぼたん.png


Step 3. Build a controller

Scene Builder menu ⇒ View ⇒ Show Sample Controller Skeleton Based on the view settings so far, it will present a template for the controller. Check Full and press Copy. Comments are your choice.

Go back to eclipse and paste the copy into the controller class. After that, you can add logic to onButtonAction. How easy it is!

Once you've done this, let's check the operation once. Right-click Main.java ⇒ Run ⇒ Java Application You should see a window with the button and text field created in step 2.

image.png


Step 4. Build business logic

I wanted to make something that could be used because it was a good idea, so I made it a tool that analyzes the topic of the sentence set. I put it on Git. Clone and run Main.java and it will work. It's easy, but please refer to it as a sample project.


Continue to distribution

This time, we even created a JavaFX application. I hope you can have an image that seems to be reachable for GUI tools. The flow of distributing the created application is described in this article.


Appendix: SceneBuider menu is garbled

Add the following sentence to SceneBuilder.cfg. If the cfg file is overwrite-protected, modify the access authority from the properties etc. and edit it.

-Duser.language=en

Reference: What to do if characters are garbled in Scene Builder


Reference material

Recommended Posts

Tool creation with JavaFX & self-contained package (JavaFX edition)
Tool creation with JavaFX & self-contained package (distribution)
Make a slideshow tool with JavaFX
HelloFX with JavaFX
Merry Christmas with JavaFX !!
Graph creation with JFreeChart