Kensoft PH
  • Blog
    • Java
    • Programming Tips
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Blog
    • Java
    • Programming Tips
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to create JavaFX Login application with CSS | 100% best for beginners

April 24, 2021 - Updated on September 21, 2022
in Java
Reading Time: 3 mins read
0
JavaFX login application
685
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • You will learn to create a JavaFX Login application with CSS
  • Source code of JavaFX Login application with CSS
    • Main class
    • FX_Login.fxml
    • FX_LoginController.java
    • Cascading Style Sheet
  •  
  • YouTube Video

You will learn to create a JavaFX Login application with CSS

You will learn how to create a simple UI and login application in JavaFX with this JavaFX login application example with CSS tutorial. When a user submits information through the login application, the information is validated to see if it is valid or not. When valid data is entered, the application displays a message dialog; otherwise, it displays an error message.

Source code of JavaFX Login application with CSS

Main class

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
*
* @author KENSOFT
*/
public class Main extends Application {

@Override
public void start(Stage stage) throws IOException {
    BorderPane root = FXMLLoader.load(getClass().getResource("FX_Login.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setTitle("Login");
    stage.setResizable(false);
    stage.show();
}

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

FX_Login.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="335.0" styleClass="borderPane_Style" stylesheets="@Styles.css" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.login.FX_LoginController">
<top>
<Label text="KENSOFT PH LOGIN" textFill="WHITE" BorderPane.alignment="CENTER">
<font>
<Font size="27.0" />
</font>
<BorderPane.margin>
<Insets top="25.0" />
</BorderPane.margin>
</Label>
</top>
<center>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<TextField fx:id="text_username" alignment="CENTER" prefHeight="45.0" prefWidth="335.0" promptText="Username">
<VBox.margin>
<Insets left="20.0" right="20.0" top="60.0" />
</VBox.margin>
<font>
<Font size="20.0" />
</font>
</TextField>
<PasswordField fx:id="text_password" alignment="CENTER" prefHeight="45.0" prefWidth="335.0" promptText="Password">
<VBox.margin>
<Insets left="20.0" right="20.0" top="20.0" />
</VBox.margin>
<font>
<Font size="20.0" />
</font>
</PasswordField>
<Button fx:id="button_login" mnemonicParsing="false" onMouseClicked="#Login" prefHeight="45.0" prefWidth="295.0" stylesheets="@Styles.css" text="Login">
<VBox.margin>
<Insets left="20.0" right="20.0" top="25.0" />
</VBox.margin>
<font>
<Font size="20.0" />
</font>
</Button>
</children>
</VBox>
</center>
</BorderPane>

FX_LoginController.java

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;

/**
* FXML Controller class
*
* @author KENSOFT
*/
public class FX_LoginController implements Initializable {

@FXML
private TextField text_username;
@FXML
private PasswordField text_password;
@FXML
private Button button_login;

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}

@FXML
private void Login(MouseEvent event) {
    if(text_username.getText().equals("admin") && text_password.getText().equals("adminpassword")){
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Login");
        alert.setContentText("Success");
        alert.setHeaderText("Login Success");
        alert.show();
    }else{
         Alert alert = new Alert(Alert.AlertType.ERROR);
         alert.setTitle("Login");
         alert.setContentText("Username or password is invalid");
         alert.setHeaderText("Login Failed");
         alert.show();
    }
  }
}

Cascading Style Sheet

.borderPane_Style {
-fx-background-color: linear-gradient(to right bottom, #002151, #00215c, #052066, #141d70, #241878);
}

.button {
-fx-background-color: white;
-fx-background-radius: 100;
-fx-text-fill: #002151;
-fx-border-width: 5;
-fx-border-color: white;
-fx-border-radius: 100;
-fx-font-weight: bold;
-fx-font-size: 20;

}

.button:hover {
-fx-background-color: transparent;
-fx-background-radius: 100;
-fx-text-fill: white;
-fx-border-width: 5;
-fx-border-color: white;
-fx-border-radius: 100;
-fx-font-weight: bold;
-fx-cursor: hand;
}

.button:pressed{
-fx-scale-z: 0.9;
-fx-scale-y: 0.9;
-fx-scale-x: 0.9;
}

 

JavaFX login application

YouTube Video

YouTube video

Previous Post

How to create JavaFX Splash Screen | 100% best for beginners

Next Post

How to set up JavaFX 16 with JDK 1.8 in NetBeans IDE | 100% best for beginners

KENSOFT

KENSOFT

My name is Kent, and KENSOFT represents a combination of my name and my passion for software development. Java is my preferred programming language, and I specialize in developing computer applications using this technology.

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
212
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
220
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
646
Next Post
Setting up JavaFX 16 with JDK 1.8

How to set up JavaFX 16 with JDK 1.8 in NetBeans IDE | 100% best for beginners

JavaFX Alert Dialogs | 100% best for beginners

JavaFX Alert Dialogs | 100% best for beginners

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

C# vs Java vs Python

C# vs Java vs Python: Which Programming Language to Learn

July 2, 2025
Complete Guide to Freelance Programming in 2025

Complete Guide to Freelance Programming in 2025

July 1, 2025 - Updated on July 3, 2025
Best IntelliJ IDEA Plugins for Developers in 2025

Best IntelliJ IDEA Plugins for Developers in 2025

June 30, 2025
top 10 common coding mistakes beginner make

Top 10 Common Coding Mistakes Beginners Make

June 29, 2025
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

My name is Kent, and KENSOFT represents a combination of my name and my passion for software development. Java is my preferred programming language, and I specialize in developing computer applications using this technology.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

C# vs Java vs Python

C# vs Java vs Python: Which Programming Language to Learn

July 2, 2025
Complete Guide to Freelance Programming in 2025

Complete Guide to Freelance Programming in 2025

July 1, 2025 - Updated on July 3, 2025
Best IntelliJ IDEA Plugins for Developers in 2025

Best IntelliJ IDEA Plugins for Developers in 2025

June 30, 2025

© 2025 Made With Love By KENSOFT PH

No Result
View All Result
  • Blog
    • Java
    • Programming Tips
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2025 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.