JavaFX Login Program GUI (graphical user interface)
JavaFX Login Program GUI (graphical user interface).
This is an example of javaFX login program. Copy this code to you IDE and use Username – Admin, And Password – 1234. If you enter correct credentials and click login, a new stage appears otherwise an alert box (Warning Dialog) Pops Up!
SOURCE CODE
JavaFXLoginProgram.java
/* * 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 javafxloginprogram; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Stage; /** * * @author HP */ public class JavaFXLoginProgram extends Application { private StackPane root = new StackPane(); //Final Username. static final String USERNAME = "Admin"; //Final Password. static final String PASSWORD = "1234"; //Username Textfield. TextField usernameTxt = new TextField(); //Password Textfield. TextField passwordTxt = new TextField(); //Declaring stage. private Stage stage; @Override public void start(Stage primaryStage) { VBox vBox = new VBox(); Button button = new Button("LOGIN"); vBox.setSpacing(8); vBox.setPadding(new Insets(10, 10, 10, 10)); vBox.getChildren().addAll( new Label("Username"), usernameTxt = new TextField(), new Label("Password"), passwordTxt, button); root.getChildren().addAll(vBox); button.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { //Printing On the console. System.out.println(usernameTxt.getText() + " " + passwordTxt.getText()); //If Credentials Are Correct. if (usernameTxt.getText().equals(USERNAME) && passwordTxt.getText().equals(PASSWORD)) { //Open New Stage. stage = new Stage(); StackPane stackPane = new StackPane(); stage.setScene(new Scene(stackPane, 200, 200)); stage.show(); } else { //Error Dialog Box/Alert!. System.out.println("Failed"); Alert alert = new Alert(AlertType.WARNING); alert.setTitle("Warning Dialog"); alert.setHeaderText("Failed"); alert.setContentText("Try Again!"); alert.showAndWait(); } } }); 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); } }