Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Create a Login Form Using Swing in Java?

Login Form in Swing

Condition for Login Form using Swing in Java

  • Description: To create the login form in Java using Swing involves using components like JFrame, JPanel, JLabel, JTextField, JPasswordField, and JButton. A JFrame acts as the main window, containing a JPanel that organizes components with a layout manager such as FlowLayout or GridBagLayout. JLabel components serve as labels for "Username" and "Password," paired with JTextField for username input and JPasswordField for password entry. Two JButton components handle user actions, one for submitting the login credentials and another for resetting the input fields. An ActionListener linked to the submit button handles input validation, comparing the credentials against predefined values or checking a database. JOptionPane can display appropriate messages indicating success or failure based on the validation outcome.
Sample Source Code
  • # LoginForm.java
    package JavaSamples;

    import javax.swing.JOptionPane;

    public class LoginForm extends javax.swing.JFrame {
    public LoginForm() {
    initComponents();
    }

    // //GEN-BEGIN:initComponents
    private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jPasswordField1 = new javax.swing.JPasswordField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Login Form");
    jLabel2.setText("UserName :");
    jLabel3.setText("Password :");

    jButton1.setText("Login");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(166, 166, 166)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addGap(38, 38, 38)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jLabel3)
    .addComponent(jLabel2))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
    .addComponent(jPasswordField1)))
    .addGroup(layout.createSequentialGroup()
    .addGap(171, 171, 171)
    .addComponent(jButton1))
    .addContainerGap(55, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(29, 29, 29)
    .addComponent(jLabel1)
    .addGap(31, 31, 31)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel2)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(50, 50, 50)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel3)
    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(30, 30, 30)
    .addComponent(jButton1)
    .addContainerGap(39, Short.MAX_VALUE))
    );
    pack();
    }
    //
    //GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String userName = jTextField1.getText();
    String password = new String(jPasswordField1.getPassword());

    if (userName.equals("SLogix") && password.equals("s123")) {
    JOptionPane.showMessageDialog(this, "Login Successfully");
    } else {
    JOptionPane.showMessageDialog(this, "Invalid Credentials");
    }
    jTextField1.setText("");
    jPasswordField1.setText("");
    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(() -> {
    new LoginForm().setVisible(true);
    });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    }
Screenshots
  • Screenshot 1: Login Form Window
  • Login Form Window

  • Screenshot 2: Input Form
  • Input Form

  • Screenshot 3: Login Successfully Message Box
  • Login Success

  • Screenshot 4: Invalid Credentials Message Box
  • Login Failed