Javafx Login Module

Create Class example Login then create new fxml.
Introduce a new anchor pane in fxml . Set its width and height.
Place a text field for user name input and a password field for user password.
Place a login button .
Introduce fx:id for each component.

Below you can see fxml code

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

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="325.0" prefWidth="483.0" style="-fx-background-color: white;" stylesheets="@simple.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
      
      <JFXTextField fx:id="usernamefld" focusColor="#f2f3f4" labelFloat="true" layoutX="133.0" layoutY="57.0" promptText="User name" stylesheets="@simple.css" unFocusColor="#f4f1f1" />
      <JFXPasswordField fx:id="passwordfld" focusColor="#f2f3f4" labelFloat="true" layoutX="133.0" layoutY="131.0" promptText="Password" stylesheets="@simple.css" unFocusColor="#f4f1f1" />
      <JFXButton fx:id="loginbtn" defaultButton="true" layoutX="166.0" layoutY="199.0" onAction="#login" prefHeight="25.0" prefWidth="69.0" text="Log In" textFill="#e07474" />
      <JFXButton fx:id="exitbtn" layoutX="384.0" layoutY="273.0" onAction="#exit" prefHeight="25.0" prefWidth="69.0" style="-fx-background-color: white;" text="Exit" textFill="#e07474" />
</AnchorPane>

Now Code for Login class created.First create a connection.Create fields for components present in fxml.Also import packages to implement these components and built in functionalities. Create connection for database server .then create a login method for implement login checking and verification of customer if present in database present in database server.

Introduce If else statements to implement checks.

Then describes what happen for user not fount in database . Else what for successfull login.

Below Class code for the module;


import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;

public class LogIn implements Initializable{

    private static Connection con = null;
    public static Statement stmt = null;

    public JFXTextField usernamefld;
    public JFXPasswordField passwordfld;
    @FXML    private JFXButton loginbtn;

    @Override    public void initialize(URL location, ResourceBundle resources) {
        createconnection();
    }
    private void createconnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
    public void login() {
        String user = usernamefld.getText();
        String pass = passwordfld.getText();
        String password = null;
        String username = null;
        if ((user.trim().isEmpty() || pass.trim().isEmpty()) || (user.isEmpty() || pass.isEmpty())) {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setContentText("User name or Password are not given");
            alert.showAndWait();
            clear();
        } else {
            try {
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM login WHERE username='"+user+"' AND password='"+pass+"'");
                while (rs.next()) {
                    username = rs.getString("username");
                   password = rs.getString("password");
                   }
                   System.out.println(username);
                System.out.println(password);


            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (username==null || password==null){
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setContentText("Incorrect Info");
                alert.showAndWait();
                clear();
            }
            else {
                loginbtn.getScene().getWindow().hide();
                Stage stage = new Stage();
                try {
                    Parent parent = FXMLLoader.load(getClass().getResource("/sample/home/home.fxml"));
                    stage.setTitle("Home");
                    Scene scene = new Scene(parent);
                    stage.setScene(scene);
                    stage.show();
                    clear();

                } catch (IOException e) {
                    e.printStackTrace();
                }

           }
   }
    }
        public void clear(){
            usernamefld.clear();
            passwordfld.clear();
            usernamefld.requestFocus();
        }

    }
Reactions

Post a Comment

0 Comments