Starting With JavaFX

Anna Scott
2 min readJun 26, 2021

JavaFX is a framework for building graphical user interfaces (GUI), it provides us with ready-to-go components like buttons or text fields.

IntelliJ IDEA is used with the support of JavaFX. To have the correct setup , please follow the directions here:

Each JavaFx program has similar structure. The main class extends the Application class, which requires overriding start() method. The start() method sets up and shows the stage (window). Within stage we need three components:

  1. Scene:

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Scene.html

2. Object (for example FlowPane) that is responsible for the arrangement of GUI components within scene (for example: buttons, labels, textfields):

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/FlowPane.html

3. GUI components:

https://docs.oracle.com/javafx/2/ui_controls/jfxpub-ui_controls.htm

Let’s look at JavaFX application:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage window){
// setting a label for our window
window.setTitle("MY GUI");

//creating GUI elements to put within objects that would be put in scene
Button button = new Button("This is a button");
TextField textField = new TextField("This is text field");
CheckBox checkBox = new CheckBox("This is check box");
Label label = new Label("This is label");

//object within scene to hold GUI elements
FlowPane flowPane = new FlowPane();

//adding GUI elements
flowPane.getChildren().add(button);
flowPane.getChildren().add(textField);
flowPane.getChildren().add(checkBox);
flowPane.getChildren().add(label);

//creating scene
Scene scene = new Scene(flowPane);

//placing scene in window
window.setScene(scene);
window.show();
}

public static void main(String[] args) {
launch(Main.class);
}
}

The result of running the above program:

Happy coding my friends!

--

--