JavaFX: Creating a second stage to show alert

This entry is part 34 of 55 in the series JavaFX

We have always used alert box to show an alert message in web development but for JavaFX, we have to roll out our own alert box.

The usual method for most JavaFX developers is to create a second stage as shown in the example below.

In this example, we will create an AlertBox class and put the class in a separate JavaFX class file.

Main Application
package javafxapplication26;

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;

public class JavaFXApplication26 extends Application {

    @Override
    public void start(Stage stage) {

        Button btn = new Button();
        btn.setText("Click Me");
        btn.setOnAction(e -> buttonClick());

        BorderPane pane = new BorderPane();
        pane.setCenter(btn);

        Scene scene = new Scene(pane, 500, 400);

        stage.setScene(scene);
        stage.setTitle("Show Alert");
        stage.show();
    }

    public void buttonClick() {

        AlertBox.show("This Is An Alert!", "Alert!");

    }

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

}

You can add a class file and name the file as AlertBox.java shown below.

AlertBox.java
package javafxapplication26;

import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.*;

public class AlertBox       
{
    public static void show(String message, String title)    
    {
        Stage stage = new Stage();       
        stage.initModality(Modality.APPLICATION_MODAL);       
        stage.setTitle(title);       
        stage.setMinWidth(400);       
        stage.setMinHeight(300);

        Label lbl = new Label();       
        lbl.setText(message);

        Button btnOK = new Button();       
        btnOK.setText("OK");
        btnOK.setMinWidth(60);
        btnOK.setOnAction(e -> stage.close());       

        VBox pane = new VBox(20);       
        pane.getChildren().addAll(lbl, btnOK);
        pane.setAlignment(Pos.CENTER);       

        Scene scene = new Scene(pane);       
        stage.setScene(scene);
        stage.showAndWait();       
    }
}

alert

 

JavaFX: Structure of a JavaFX application

This entry is part 3 of 55 in the series JavaFX

Using the example created by Netbeans, let’s look the structure of a JavaFX application.

The class that we have created here is called JavaFXApplication5, this is an extension of the main class javafx.application.Application.

JavaFX applications need a start() method but not the main() method. But for backward compatibility with Java Swing, we always include the main() method in the application.

The UI Interface of a FX application always contains a stage and scene.  The stage is the top-level or outermost container.  Inside the outermost container is the scene.

When we create a button, we are creating it in the scene.

Structure of A JavaFX App
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplication5;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author daryl
 */
public class JavaFXApplication5 extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

j3