Creating a basic JavaFX application

Creating a basic JavaFX application

... stuff that belongs in CSS, done in Java

JavaFX is a Java library and a GUI toolkit to develop web and desktop applications that can be run on multiple platforms such as Linux, Windows, macOS, etc. It displays a valuable collection of graphics and various media APIs. For this very reason, to combine graphics, animations, and UI control in applications JavaFX is preferred.

Implementing a JavaFX application with IntelliJ IDEA

~ ( IntelliJ IDEA ide). To get started, create a new project selecting JavaFX as the generator.

Once you create the project, open the .java file and start writing your code after the JavaFX package import statement (package com.example.javafx;) Other than this, we also need to import the javafx.application.Application class which contains the start() method, the entry point of any JavaFX application as well as the javafx.stage.Stage; class which forms the parameter for the start() method.

A Stage object represents a window on the computer's screen. The stage that is passed as a parameter to start() is constructed by the system. It represents the main window of a program, and is often referred to as the "primary stage." A program can create other windows by constructing new objects of type Stage.

Once everything has been imported, we need to create a public class. Since this is going to be an application, our class needs to extend an application. Inside the class, there will be two methods. A main method (makes things easier to call) and a start method that is included in the javafx.application.Application; class. Your code should look something like this:

Any element that we now create in the start method will be added to a scene() which will then be added to the stage that is passed to the start() method at the top. The stage, which creates a window on the screen can contain many components such as menus, buttons, input boxes or even drawing areas for 2d or 3d shapes.

Let's see how you can create some of these components:

1.Creating shapes :

JavaFX provides a variety of shape classes like Rectangle, Circle, Line, and Ellipse that you can use to draw basic shapes. For example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

public class ShapeDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Create shapes
        Rectangle rectangle = new Rectangle(50, 50, 100, 75);
        rectangle.setStyle("-fx-fill: lightblue;");

        Circle circle = new Circle(200, 100, 50);
        circle.setStyle("-fx-fill: lightgreen;");

        Line line = new Line(50, 200, 250, 200);
        line.setStyle("-fx-stroke: black;");

        Ellipse ellipse = new Ellipse(150, 300, 80, 50);
        ellipse.setStyle("-fx-fill: lightcoral;");

        // Create a layout container
        Pane pane = new Pane();
        pane.getChildren().addAll(rectangle, circle, line, ellipse);

        // Create a Scene and set it on the Stage
        Scene scene = new Scene(pane, 300, 400);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX Shapes");
        primaryStage.show();
    }

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

Here:

Rectangle: Created with position (50, 50) and size (100, 75).

  • Circle: Centered at (200, 100) with a radius of 50.

  • Line: Drawn from (50, 200) to (250, 200).

  • Ellipse: Centered at (150, 300) with radii 80 and 50.

2.Buttons and events :

Buttons are triggers used to perform certain actions, that can be customized using event handlers. Let's look at an example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button("Click Me");
        button.setOnAction(e -> button.setText("Button Clicked!"));

        VBox vbox = new VBox(button);
        vbox.setSpacing(10);

        Scene scene = new Scene(vbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX Button");
        primaryStage.show();
    }

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

3.Creating Menus

A menu bar, commonly seen in navbars of websites, contains items in a certain order to make them more accessible. Here's how you can create one with file and edit menus :

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MenuDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Create menu items
        MenuItem newItem = new MenuItem("New");
        MenuItem openItem = new MenuItem("Open");
        MenuItem saveItem = new MenuItem("Save");
        MenuItem cutItem = new MenuItem("Cut");
        MenuItem copyItem = new MenuItem("Copy");
        MenuItem pasteItem = new MenuItem("Paste");

        // Create menus
        Menu fileMenu = new Menu("File", null, newItem, openItem, saveItem);
        Menu editMenu = new Menu("Edit", null, cutItem, copyItem, pasteItem);

        // Create menu bar
        MenuBar menuBar = new MenuBar(fileMenu, editMenu);

        // Create a layout container
        BorderPane borderPane = new BorderPane();
        borderPane.setTop(menuBar);

        // Create a Scene and set it on the Stage
        Scene scene = new Scene(borderPane, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX Menu");
        primaryStage.show();
    }

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

To run these, simply compile and execute to see your window containing all the different components.

Thanks for reading, hope this taught you something new :)