How to Create Simple Login Page in Java Swing/AWT/SWT/Windows Builder

Last modified date

People who are new to Java Swing are little eager to create a small application, but the only thing is what are the components to be used and working with them does actually matters. Its never hard for people working with the components like dragging and dropping JTextField, JPasswordField, JButton and etc. But actually what after that how to get the field values and validate them to complete and login page. So don’t worry guys  it never hard for you people once you learn it.

package ccp;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JDialog {

  private final JPanel contentPanel = new JPanel();
  private JTextField username;
  private JPasswordField password;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    try {
      Login dialog = new Login();
      dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      dialog.setVisible(true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Create the dialog.
   */
  public Login() {
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(null);
    {
      JLabel lblUsername = new JLabel("UserName");
      lblUsername.setBounds(89, 76, 63, 20);
      contentPanel.add(lblUsername);
    }
    {
      JLabel lblPassword = new JLabel("Password");
      lblPassword.setBounds(89, 119, 63, 20);
      contentPanel.add(lblPassword);
    }
    
    username = new JTextField();
    username.setBounds(173, 76, 152, 20);
    contentPanel.add(username);
    username.setColumns(10);
    
    password = new JPasswordField();
    password.setBounds(173, 119, 152, 20);
    contentPanel.add(password);
    
    JButton btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        if(username.getText().equals("HELLO") && password.getText().equals("WORLD") ) {
          JOptionPane.showMessageDialog(null, "Login Sucessful ");
          //YOUR CODE GOES HERE
        }else
        {
          JOptionPane.showMessageDialog(null, "Wrong Inputs", "Please Check", JOptionPane.WARNING_MESSAGE);
          return;
        }
      }
    });
    btnLogin.setBounds(205, 165, 89, 23);
    contentPanel.add(btnLogin);
    
    JLabel lblLogin = new JLabel("Login");
    lblLogin.setHorizontalAlignment(SwingConstants.CENTER);
    lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblLogin.setBounds(104, 23, 230, 23);
    contentPanel.add(lblLogin);
  }
}

To validate the field username and password, the text of those components by default String, so by getting the String we must validate them with our specific String.

In the above code if you clearly observe i have only taken to inputs username and password. I have validated at the action performed of JButton Login. You can directly copy paste the code in any class with Swing environment Eclipse and run it, you will have a clear idea of working the code.

i have given the exception with warning message in else condition and return if wrong.

intactabode