Graphical User Interface Login Page In Java

Graphical User Interface

A Graphical User Interface is basically the interface or interactive interface for a web page so that the user can easily do what they want to. A GUI contains various types of buttons, links and so on that makes it easy to access the contents of the pages from every field of the web.

Here we are making a GUI login page in which the login is authenticated through the database for which database connectivity is done in our example.

Example

In this example we are creating the following three Java files.

  • AdminHome.java
  • Main.java
  • CONN.java

In the AdminHome.java file we are defining everything required for the authentication.

The Main.java file runs the entire concept with the main class MyCompany.

The CONN.java file is used to connect the files with a database to do the authentication process for a proper login.

Basically this example is based on the MyCompany package in which there are employees, managers and admins who can login safely and the admins have all the ability to check all the details of all the managers and employees. That is why we created the AdminHome page only for the user interface from where employees and managers also can login through a drop down list and do their work.

In the login page, maintenance of the validation is also done to check whether the user has filled in the required fields. If all the fields are not filled in then there will be an error message shown in the pop-up box.

For doing this, various API packages are also needed and some of the packages are as follows:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import mysqlCONN.CONN;

import java.sql.*;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

import javax.swing.table.TableRowSorter;

import java.util.Vector;

You will also need to maintain the database through which authentication can be done. You need to make the individual table for admins, employees and managers in which all the details should be there.
The Admin also provided the authority to edit the details of him as well as of the employee and the manager.

Now let's understand the example.

AdminHome.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import mysqlCONN.CONN;

import java.sql.*;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

import javax.swing.table.TableRowSorter;

import java.util.Vector;

 

public class AdminHome extends JFrame {

 

    public AdminHome()throws Exception{
 

        super("Admin Home");

        Vector row;

        Vector data=new Vector();

        CONN ob=new CONN();

        Connection con=ob.c();

        Statement stm=con.createStatement();

        ResultSet rst=stm.executeQuery("select * from employee");

 

        while(rst.next())

        {

            row=new Vector();

            row.add(rst.getString("EmpID"));

            row.add(rst.getString("username"));

            row.add(rst.getString("password"));

            row.add(rst.getString("name"));

            row.add(rst.getString("address"));

            row.add(rst.getString("contact"));

            row.add(rst.getString("salary"));

            data.add(row);

        }

 

        Vector cols=new Vector();

        cols.add("Employee ID");

        cols.add("Username");

        cols.add("Password");

        cols.add("Name");

        cols.add("Address");

        cols.add("Contact");

        cols.add("Salary");

 

        TableModel model=new DefaultTableModel(data,cols);

        JTable table=new JTable(model);

        final TableRowSorter<TableModel> sorter;

        sorter=new TableRowSorter<TableModel>(model);

        table.setRowSorter(sorter);

        getContentPane().add(new JScrollPane(table));

        JPanel pnl=new JPanel();

        pnl.add(new JLabel("Search expression"));

        final JTextField txtFE=new JTextField(25);

        pnl.add(txtFE);

        final JButton btnSetFE=new JButton("Search");

        final JButton btnAdd=new JButton("Add Emp"); 

 

        ActionListener a1;

        a1=new ActionListener()

        {

            public void actionPerformed(ActionEvent e)

            {

                if(e.getSource()==btnSetFE)

                {

                    String expr=txtFE.getText();

                    sorter.setRowFilter(RowFilter.regexFilter(expr));

                    sorter.setSortKeys(null);

                }

                if(e.getSource()==btnAdd)

                {

 

                }

            } 

        };

 

        btnSetFE.addActionListener(a1);

        btnAdd.addActionListener(a1);

        pnl.add(btnSetFE);

        pnl.add(btnAdd);

        getContentPane().add(pnl,BorderLayout.SOUTH);

        Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();

 

        this.setSize(dim.width,dim.height-40);

        this.setVisible(true);

        this.setLayout(null);

        this.setResizable(true);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

}

Main.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import mysqlCONN.CONN;

import java.sql.*;

 

class MyCompany extends JFrame implements ActionListener

{

    JLabel lblCompany,lblUser_Type,lblUser,lblPass;

    JComboBox CmbUser_Type;

    JTextField txtUser;

    JPasswordField txtPass;

    JButton btnLogin,btnCancel;

    String arr[]={"Admin","Manager","Employee"};

 

    public MyCompany()

    {

        super("TATA");

        Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();

 

        this.setSize(dim.width, dim.height);

        this.setLayout(null);

        this.setResizable(true);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

 

        lblCompany=new JLabel("MyCompany");

        lblCompany.setFont(new Font("Times New Roman",Font.BOLD,30));

        lblCompany.setForeground(Color.blue);

        lblUser_Type=new JLabel("UserType");

        lblUser=new JLabel("Username");

        lblPass=new JLabel("Password");

        txtUser=new JTextField();

        txtPass=new JPasswordField();

        CmbUser_Type=new JComboBox(arr);

        btnLogin=new JButton("Login");

        btnCancel=new JButton("Cancel");

 

        lblCompany.setBounds(550,300,200,30);

        lblUser_Type.setBounds(50036014025);

        CmbUser_Type.setBounds(600,360,130,25);

        lblUser.setBounds(500,400,140,25);

        txtUser.setBounds(600,400,130,25);

        lblPass.setBounds(500,440,140,25);

        txtPass.setBounds(600,440,140,25);

        btnLogin.setBounds(500,480,100,30);

        btnCancel.setBounds(600,480,100,30);

 

        this.add(btnCancel);

        this.add(btnLogin);

        this.add(CmbUser_Type);

        this.add(lblCompany);

        this.add(lblPass);

        this.add(lblUser);

        this.add(txtUser);

        this.add(lblUser_Type);

        this.add(txtPass);

 

        btnCancel.addActionListener(this);

        btnLogin.addActionListener(this);

    }

 

    public void actionPerformed(ActionEvent e){

 

        String u=txtUser.getText();

        String p=txtPass.getText();

        String ut=(String)CmbUser_Type.getSelectedItem();

 

        if(e.getSource()==btnLogin)

        {

            if(u.equals("")||u==null)

            {

                JOptionPane.showMessageDialog(rootPane, "Fill the username");

            }

            else

            {

                if(p.equals("")||p==null)

                {

                    JOptionPane.showMessageDialog(rootPane, "Fill the password");

                }

            }

        }

        if(e.getSource()==btnCancel)

        {

            System.exit(0);

        }

        try

        {

            CONN ob=new CONN();

            Connection con=ob.c();

            Statement stm=con.createStatement();

            ResultSet rst = null;

 

            if(ut.equals("Admin"))

            {

                rst=stm.executeQuery("select * from admin where username='"+u+"' and password='"+p+"'");

                if(rst.next())

                {

                    AdminHome ob1=new AdminHome();

                    ob1.setVisible(true);

                    this.dispose();

                }

                else

                {

                    JOptionPane.showMessageDialog(rootPane, "Incorrect username or password");

                }

 

                if(ut.equals("Customer"))

                {

                    rst=stm.executeQuery("select * from customer where username='"+u+"' and password='"+p+"'");

                }

                if(ut.equals("employee"));

                {

                    rst=stm.executeQuery("select * from employee where username='"+u+"' and password='"+p+"'");

                }

            }

        }

        catch (Exception e1)

        {

            JOptionPane.showMessageDialog(rootPane, e1);

        }

    }

}

 

public class Main {

 

    public static void main(String[] args)

    {

        MyCompany ob=new MyCompany();

        ob.setVisible(true);

    }

}

CONN.java

import java.sql.*;

 

public class CONN {

 

    public Connection c() throws Exception {

        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bankdb""root""root");

        return con;

    }

}

Output

Output in Java

Output with drop down list

Output with DropDownList in Java

Output with pop-up box of filling the username

Popup Window with UserName

Output with pop-up box of filling the password

Popup Window with Password

Up Next
    Ebook Download
    View all
    Learn
    View all