Graphical User Interface Login Page In Java

Graphical User Interface

 
A Graphical User Interface is the interface, or interactive interface, for a web page, so that the user can easily do what they want.  A GUI contains various types of buttons, links, and so on that make 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 log in safely.  The admins have the ability to check 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 can log in 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 of 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 needed.  Some of the packages are as follows:
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import mysqlCONN.CONN;  
  5. import java.sql.*;  
  6. import javax.swing.table.DefaultTableModel;  
  7. import javax.swing.table.TableModel;  
  8. import javax.swing.table.TableRowSorter;  
  9. 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

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import mysqlCONN.CONN;  
  5. import java.sql.*;  
  6. import javax.swing.table.DefaultTableModel;  
  7. import javax.swing.table.TableModel;  
  8. import javax.swing.table.TableRowSorter;  
  9. import java.util.Vector;  
  10. public class AdminHome extends JFrame {  
  11.  public AdminHome() throws Exception {  
  12.   super("Admin Home");  
  13.   Vector row;  
  14.   Vector data = new Vector();  
  15.   CONN ob = new CONN();  
  16.   Connection con = ob.c();  
  17.   Statement stm = con.createStatement();  
  18.   ResultSet rst = stm.executeQuery("select * from employee");  
  19.   while (rst.next()) {  
  20.    row = new Vector();  
  21.    row.add(rst.getString("EmpID"));  
  22.    row.add(rst.getString("username"));  
  23.    row.add(rst.getString("password"));  
  24.    row.add(rst.getString("name"));  
  25.    row.add(rst.getString("address"));  
  26.    row.add(rst.getString("contact"));  
  27.    row.add(rst.getString("salary"));  
  28.    data.add(row);  
  29.   }  
  30.   Vector cols = new Vector();  
  31.   cols.add("Employee ID");  
  32.   cols.add("Username");  
  33.   cols.add("Password");  
  34.   cols.add("Name");  
  35.   cols.add("Address");  
  36.   cols.add("Contact");  
  37.   cols.add("Salary");  
  38.   TableModel model = new DefaultTableModel(data, cols);  
  39.   JTable table = new JTable(model);  
  40.   final TableRowSorter < TableModel > sorter;  
  41.   sorter = new TableRowSorter < TableModel > (model);  
  42.   table.setRowSorter(sorter);  
  43.   getContentPane().add(new JScrollPane(table));  
  44.   JPanel pnl = new JPanel();  
  45.   pnl.add(new JLabel("Search expression"));  
  46.   final JTextField txtFE = new JTextField(25);  
  47.   pnl.add(txtFE);  
  48.   final JButton btnSetFE = new JButton("Search");  
  49.   final JButton btnAdd = new JButton("Add Emp");  
  50.   ActionListener a1;  
  51.   a1 = new ActionListener() {  
  52.    public void actionPerformed(ActionEvent e) {  
  53.     if (e.getSource() == btnSetFE) {  
  54.      String expr = txtFE.getText();  
  55.      sorter.setRowFilter(RowFilter.regexFilter(expr));  
  56.      sorter.setSortKeys(null);  
  57.     }  
  58.     if (e.getSource() == btnAdd) {}  
  59.    }  
  60.   };  
  61.   btnSetFE.addActionListener(a1);  
  62.   btnAdd.addActionListener(a1);  
  63.   pnl.add(btnSetFE);  
  64.   pnl.add(btnAdd);  
  65.   getContentPane().add(pnl, BorderLayout.SOUTH);  
  66.   Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();  
  67.   this.setSize(dim.width, dim.height - 40);  
  68.   this.setVisible(true);  
  69.   this.setLayout(null);  
  70.   this.setResizable(true);  
  71.   this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  72.  }  
  73. }  
 

Main.java

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. import mysqlCONN.CONN;  
  5. import java.sql.*;  
  6. import javax.swing.table.DefaultTableModel;  
  7. import javax.swing.table.TableModel;  
  8. import javax.swing.table.TableRowSorter;  
  9. import java.util.Vector;  
  10. public class AdminHome extends JFrame {  
  11.  public AdminHome() throws Exception {  
  12.   super("Admin Home");  
  13.   Vector row;  
  14.   Vector data = new Vector();  
  15.   CONN ob = new CONN();  
  16.   Connection con = ob.c();  
  17.   Statement stm = con.createStatement();  
  18.   ResultSet rst = stm.executeQuery("select * from employee");  
  19.   while (rst.next()) {  
  20.    row = new Vector();  
  21.    row.add(rst.getString("EmpID"));  
  22.    row.add(rst.getString("username"));  
  23.    row.add(rst.getString("password"));  
  24.    row.add(rst.getString("name"));  
  25.    row.add(rst.getString("address"));  
  26.    row.add(rst.getString("contact"));  
  27.    row.add(rst.getString("salary"));  
  28.    data.add(row);  
  29.   }  
  30.   Vector cols = new Vector();  
  31.   cols.add("Employee ID");  
  32.   cols.add("Username");  
  33.   cols.add("Password");  
  34.   cols.add("Name");  
  35.   cols.add("Address");  
  36.   cols.add("Contact");  
  37.   cols.add("Salary");  
  38.   TableModel model = new DefaultTableModel(data, cols);  
  39.   JTable table = new JTable(model);  
  40.   final TableRowSorter < TableModel > sorter;  
  41.   sorter = new TableRowSorter < TableModel > (model);  
  42.   table.setRowSorter(sorter);  
  43.   getContentPane().add(new JScrollPane(table));  
  44.   JPanel pnl = new JPanel();  
  45.   pnl.add(new JLabel("Search expression"));  
  46.   final JTextField txtFE = new JTextField(25);  
  47.   pnl.add(txtFE);  
  48.   final JButton btnSetFE = new JButton("Search");  
  49.   final JButton btnAdd = new JButton("Add Emp");  
  50.   ActionListener a1;  
  51.   a1 = new ActionListener() {  
  52.    public void actionPerformed(ActionEvent e) {  
  53.     if (e.getSource() == btnSetFE) {  
  54.      String expr = txtFE.getText();  
  55.      sorter.setRowFilter(RowFilter.regexFilter(expr));  
  56.      sorter.setSortKeys(null);  
  57.     }  
  58.     if (e.getSource() == btnAdd) {}  
  59.    }  
  60.   };  
  61.   btnSetFE.addActionListener(a1);  
  62.   btnAdd.addActionListener(a1);  
  63.   pnl.add(btnSetFE);  
  64.   pnl.add(btnAdd);  
  65.   getContentPane().add(pnl, BorderLayout.SOUTH);  
  66.   Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();  
  67.   this.setSize(dim.width, dim.height - 40);  
  68.   this.setVisible(true);  
  69.   this.setLayout(null);  
  70.   this.setResizable(true);  
  71.   this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  72.  }  
  73. }  
 

CONN.java

  1. import java.sql.*;  
  2. public class CONN {  
  3.  public Connection c() throws Exception {  
  4.   Class.forName("com.mysql.jdbc.Driver");  
  5.   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bankdb""root""root");  
  6.   return con;  
  7.  }  
  8. }   

Output

 
Output in Java
 

Output with dropdown list

 
Output with DropDownList in Java

 
 

Output with a pop-up box of filling the username

 
Popup Window with UserName
 

Output with a pop-up box of filling the password

 
Popup Window with Password