Last Updated:

How to Write an Application Using JavaFX

I decided to write an executable jar file with a user-friendly interface for calculating my daily work tasks. I started with the simplest ones.

It is necessary to determine the critical shear stresses based on the type of fixation and the ratio of the sides of the rectangular plate.

Customize the interface with Scene Builder

Let's create a workspace with a size of 900x600.

Scene Builder - JavaFx

Let's create several containers with sizes and indentations (at first I did the markup just on a piece of paper, but the leaf was already lost).


Internal elements of containers

Let's assign a unique name to our button and the name of the method in which we will then implement our code

Let's give a unique name to our text field. The field is needed for input/output of numeric values. We will also add an additional name to the field, which will disappear when you type the character

Field I used the Label field to denote cells.

field Here we specify the name of the existing image located in the project

Field Specify a unique name here. this is necessary to determine the type of pinning

The final code of the sample.fxml file

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.pane?>
<?import javafx.scene.layout.VBox?>

<Pane prefHeight="600.0" prefWidth="900.0" xmlns="" xmlns:fx="" fx:controller= "sample.Controller">

<Button fx:id="button" layoutX="40.0" layoutY="200.0" mnemonicParsing="false" onAction="#showDateTime" prefHeight="71.0" prefWidth="528.0" text="=" />

<VBox layoutX="312.0" layoutY="20.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="280.0" GridPane.columnIndex="1">
<TextField fx:id="myTextField1" promptText="Value a" />
<TextField fx:id="myTextField2" promptText="b value (smallest side)" />
<TextField fx:id="myTextField3" promptText="Thickness" />
<TextField fx:id="myTextField4" prefHeight="25.0" prefWidth="280.0" promptText="Modulus E" />
<TextField fx:id="myTextField6" prefHeight="25.0" prefWidth="280.0" promptText="Actual stresses" />
<VBox layoutX="611.0" prefHeight="562.0" prefWidth="271.0" GridPane.columnIndex="21">
<ImageView fx:id="image" fitHeight="116.0" fitWidth="174.0" pickOnBounds="true" preserveRatio="true">
<Image url="@Zadelka1.PNG" />
<CheckBox fx:id="checkbox1" mnemonicParsing="false" text="All edges pinched" />
<ImageView fitHeight="122.0" fitWidth="237.0" layoutX="30.0" pickOnBounds="true" preserveRatio="true">
<Image url="@Zadelka2.PNG" />
<CheckBox fx:id="checkbox2" mnemonicParsing="false" text="Hinge on top and bottom edges" />
<ImageView fitHeight="127.0" fitWidth="272.0" pickOnBounds="true" preserveRatio="true">
<Image url="@Zadelka3.PNG" />
<CheckBox fx:id="checkbox3" mnemonicParsing="false" text="Hinged on the sides" />
<ImageView fitHeight="129.0" fitWidth="392.0" pickOnBounds="true" preserveRatio="true">
<Image url="@Zadelka4.PNG" />
<CheckBox fx:id="checkbox4" mnemonicParsing="false" text="Hinge around" />

<VBox layoutX="20.0" layoutY="20.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="262.0">
<Label prefHeight="25.0" prefWidth="185.0" text="Value a" />
<Label prefHeight="25.0" prefWidth="262.0" text="Value b (smallest side)" />
<Label prefHeight="25.0" prefWidth="188.0" text="Thickness" />
<Label prefHeight="25.0" prefWidth="186.0" text="Modulus E" />
<Label prefHeight="25.0" prefWidth="188.0" text="Effective stresses" />
<VBox layoutX="312.0" layoutY="311.0" prefHeight="200.0" prefWidth="280.0">
<TextField fx:id="myTextField5" prefHeight="25.0" prefWidth="280.0" promptText="Shear stress(cr)" />
<TextField fx:id="myTextField7" prefHeight="25.0" prefWidth="280.0" promptText="factor of safety" />
<VBox layoutX="20.0" layoutY="310.0" prefHeight="200.0" prefWidth="262.0">
<Label prefHeight="25.0" prefWidth="260.0" text="Critical shear stress" />
<Label prefHeight="25.0" prefWidth="187.0" text="Hurl" />



To create the main method of the Main class


package sample;

import javafx.application.Application;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javafx.stage.Stage;

public class Main extends Application {


public void start(StageprimaryStage) {
try {
// Link to sample.fxml (our interface). The name of the program and the creation of our main scene
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Pure Stage");
primaryStage.setScene(new Scene(root));;

} catch(Exception e) {

public static void main(String[] args) {

Creating a Controller Class.Most Important Is Marked with Comments


Creating Two Classes to Apply a Data Entry Filter


Create two classes for applying an input filter (Letter exclusion, as well as the ability to limit the number of inputs/outputs). The solution is not mine, but I found it on stackoverflow.Of all the proposed ones, it seemed to me the simplest and most understandable

AlphaNumericTextFormatter class

MaxLengthTextFormatter class

Finally Create a jar file.

That's it. Now you have a ready file that will run on any computer with java installed.