Research breakthrough possible @S-Logix pro@slogix.in

Office Address

Social List

How to Create a Registration Form Using Swing in Java?

Registration Form in Swing

Condition for Registration Form using Swing in Java

  • Description: To create a registration form using Swing in Java, components such as JFrame, JPanel, JLabel, JTextField, JPasswordField, and JButton are essential. A JFrame acts as the main window, with a JPanel inside it arranged using a layout manager like GridLayout or GridBagLayout for organizing the components. JLabel components provide field labels such as "Name," "Email," "Password," and "Phone Number," paired with JTextField or JPasswordField for user input. JButton elements handle actions like submitting the form or resetting the fields. An ActionListener attached to the submit button processes user input, performing validation and optionally saving the data to a database or file. JOptionPane displays success or error messages based on the input validation results.
Sample Source Code
  • # RegistrationForm.java
    package JavaSamples;

    import javax.swing.JOptionPane;

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

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

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Registration Form");
    jLabel2.setText("Name :");
    jLabel3.setText("Email :");
    jLabel4.setText("Password :");
    jLabel5.setText("Ph No :");

    jButton1.setText("Submit");
    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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
    .addGap(0, 0, Short.MAX_VALUE)
    .addComponent(jButton1)
    .addGap(188, 188, 188))
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(157, 157, 157)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addGap(55, 55, 55)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jLabel3)
    .addComponent(jLabel2)
    .addComponent(jLabel4)
    .addComponent(jLabel5))
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(jTextField1)
    .addComponent(jTextField2)
    .addComponent(jPasswordField1)
    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))))
    .addContainerGap(64, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(25, 25, 25)
    .addComponent(jLabel1)
    .addGap(35, 35, 35)
    .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(37, 37, 37)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel3)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(36, 36, 36)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel4)
    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(32, 32, 32)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jLabel5)
    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addComponent(jButton1)
    .addContainerGap(82, Short.MAX_VALUE))
    );
    pack();
    }
    //
    //GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String name = jTextField1.getText();
    String email = jTextField2.getText();
    String password = new String(jPasswordField1.getPassword());
    String ph = jTextField3.getText();

    JOptionPane.showMessageDialog(this, "Registered Successfully!\n"
    + "Name: " + name + "\nEmail: " + email + "\nPassword: " + password + "\nPhone: " + ph);
    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new RegistrationForm().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.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JPasswordField jPasswordField1;
    }
Screenshots
  • Screenshot 1: Registration Form Window
  • Registration Form Window

  • Screenshot 2: Input Form
  • Input Form

  • Screenshot 3: Success Message Box
  • Success Message Box

  • Screenshot 4: Reset Form
  • Reset Form