[Video] Java Fx Programming - Creating Java Fx Login Application

in #utopian-io6 years ago

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

Creating Java Fx Login Application

  • Create UI
  • Button Event Handling;
    1. Lambda Expression
    2. Method Reference
  • Database Connection
  • Login Trial Counter

Requirements

Difficulty

  • Intermediate

Description

In this tutorial I have explained the process of building a login application with Java Fx, code and video below. It is required to download the tools used as they are mandatory for code to work. Questions would be addressed.


Code Segment 1
Creating the UI

   VBox root = new VBox();
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);
connect = databaseConnection();

        TextField usernamefield = new TextField();
        PasswordField passwordField = new PasswordField();
        Button loginbtn = new Button("Login/Sign IN");
        Label outputlabel = new Label("This displays the output");

root.getChildren().addAll(usernamefield,passwordField,loginbtn,outputlabel);
        primaryStage.setTitle("Login App");
        primaryStage.setScene(new Scene(root, 300, 150));
        primaryStage.show();

Code Segment 2
Connection to Database

public static Connection databaseConnection(){
        try{
          Class.forName("org.sqlite.JDBC");
          Connection connect = DriverManager.getConnection("jdbc:sqlite:UserDetails.db");
          System.out.print("Database Connected");
return connect;


        }catch(Exception e){

            e.printStackTrace();
            return null;
        }
    }


Code Segment 3
Login with Lambda Expression

 loginbtn.setOnAction(e ->{

            String username = usernamefield.getText();
            String password = passwordField.getText();

try{

    String sql = "Select * from Login where Username = ? and Password =?";
    pst = connect.prepareStatement(sql);
    pst.setString(1, username);
    pst.setString(2, password);
    rst = pst.executeQuery();
    if(rst.next()){

        outputlabel.setText("User details are correct");
    }
    else{

        outputlabel.setText("User details are incorrect");
 loginattempts.set(loginattempts.add(1).get());
        if(loginattempts.get()>2){

            usernamefield.setEditable(false);
            passwordField.setEditable(false);

            timer();
            outputlabel.setText("Please try again");
            usernamefield.setEditable(true);
            passwordField.setEditable(true);


        }
    }

Code Segment 4
Timer method

private void timer(){
// this method handles counting the seconds after maximum trials nuumber is reached.
        long start = System.currentTimeMillis();
       long end = start + maxattempts *1000;
        while(System.currentTimeMillis()<end){

        }
    }


Code Segment 5
Imports and Variables

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.*;


// Create the username and password to be used for validation before database validation.
private static String user = "Username";
    private static String pass = "Password";
    
// Create [Integer property](https://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/IntegerProperty.html) used in the restriction after specified maximum trials.
private final static IntegerProperty loginattempts = new SimpleIntegerProperty();
    private int maxattempts = 5;

// Create variables required for database connection and validation [More information](https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html)
Connection connect = null;
PreparedStatement pst = null;
ResultSet rst =null;

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/officialhord/JavaFxTutorial/tree/master/LoginProject

Sort:  
Loading...

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64266.94
ETH 3077.24
USDT 1.00
SBD 3.87