Login Validation Program

11/12/2011 code-examplejavajava-swing

# Summary

This is a small JAVA Swing GUI login validation program which takes username & password from user and compares with the default values pre-defined. If successful then shows success dialog box else shows failed dialog box. Various swing components are used in making of this program and specially JPasswordField() is the class used to display a password textfield.

# Code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swingdemo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Rohan
 */
public class LoginDemo extends JFrame implements ActionListener {

    String default_username, default_password;
    JLabel l_username, l_password;
    JTextField t_username;
    JPasswordField t_password;
    JButton b_verify, b_reset;
    JPanel p1;

    public LoginDemo() {
        default_username = "student";
        default_password = "stu@dents1";
        Container cp = getContentPane();
        l_username = new JLabel("Username: ");
        l_password = new JLabel("Password: ");
        t_username = new JTextField(15);
        t_password = new JPasswordField(15);
        b_verify = new JButton("Verify");
        b_reset = new JButton("Reset");
        p1 = new JPanel();
        p1.add(l_username);
        p1.add(t_username);
        p1.add(l_password);
        p1.add(t_password);
        p1.add(b_verify);
        p1.add(b_reset);

        b_verify.addActionListener(this);
        b_reset.addActionListener(this);

        cp.add(p1, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        LoginDemo ld = new LoginDemo();
        ld.setTitle("Login Validation Program");
        ld.setVisible(true);
        ld.setSize(300, 300);
        ld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b_verify) {
            String username = "";
            String password = "";
            if (!t_username.getText().equals("")
                    && !t_password.getText().equals("")) {
                username = t_username.getText();
                password = t_password.getText();
                if (username.equals(default_username) && password.equals(default_password)) {
                    JOptionPane.showMessageDialog(null, "Login was successful");
                } else {
                    JOptionPane.showMessageDialog(null, "Login Failed");
                }
            } else {
                if (!t_username.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Username required");
                } else if (!t_password.getPassword().toString().equals("")) {
                    JOptionPane.showMessageDialog(null, "Password required");
                }
            }
        } else if (e.getSource() == b_reset) {
            t_username.setText("");
            t_password.setText("");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82