Registration Form in Windows Form Using Swing in Java

Introduction

This article explains how to create a Registration Form in a Windows Forms application using Swing in Java. The NetBeans IDE is used to create this application.

Registration Form in Windows Forms application

For creating this application, we need the following files:
  1. Java file
  2. SQL file
  3. odjbc.jar file
  4. NetBeans IDE

1. Java File

This Java file is necessary for writing the code. In this file we use Swing components to create a registration form with password validation.

What we can do

First we can import several packages.

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

The Swing package is used for the Swing components. All Swing components are defined within this package. The AWT package provides an event handling mechanism, in other words it deals with events like "Button Click". The SQL package creates the JDBC connection.

B. Extends the JFrame components and implements the ActionListener.

Syntax

class EmpSearchApp extends JFrame implements ActionListener

C. Now Declare following components

    JLabel l1, l2, l3, l4, l5, l6, l7, l8;

    JTextField tf1, tf2, tf5, tf6, tf7;

    JButton btn1, btn2;

    JPasswordField p1, p2; 

D. Now declare Frame components in a default constructor.

Syntax

  1. RegistrationFormApp()  
  2. {  
  3. ...  
  4. ...  
  5. try{  
  6. //JDBC CODE  
  7. }Catch(Exception ex)  
  8. {  
  9. System.out.println(ex)  
  10. }  
  11. ...  
  12. }  
Note: In the dotted part we declare and add various components of Swing; in this part the JDBC code is also used to save user records in a database named "reg". The full code I'll show you below, but here I'll only summarize for you what I can do.

D. Add an ActionListener for the button clicked event

  1. public void actionPerformed(ActionEvent e)  
  2. {  
  3.     ...  
  4.     ...  
  5. }  
Note: Since we have multiple buttons we can use "if (e.getSource() == buttonName)".

E. Create main method and run the constructor

Finally, create a main method and run the constructor as in the following:

  1. public static void main(String arr[])   
  2. {  
  3.     new RegistrationFormApp();  
  4. }  
2. ojdbc.jar file

 

This JAR file provides a way to set up a Java connection with an Oracle Database. Since the JDBC connection is provided by the Oracle Server vendor, we need to import this JAR file to our library folder.

3. reg.sql table

For fetching records we need a database table; for that we create an "emp" table in our "sandeep" database.

reg.sql

  1. create table emp  
  2. (  
  3.     name varchar2(30), email varchar2(40),  
  4.     pass varchar2(30), count varchar2(30),  
  5.     state varchar2(30), phone number(15)  
  6. );  

Now let's start creating this app. Use the following procedure to do that in the NetBeans IDE.

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Choose "Java" -> "Java application" as shown below

Java Application

Step 3

Type your project name as "RegistrationFormApp" as in the following and click on "finish".

RegistrationFormApp

Step 4

Create a new Java Class "Registration" and write the following there.

Registration.java

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. import java.sql.*;  
  5. public class Registration extends JFrame implements ActionListener   
  6. {  
  7.     JLabel l1, l2, l3, l4, l5, l6, l7, l8;  
  8.     JTextField tf1, tf2, tf5, tf6, tf7;  
  9.     JButton btn1, btn2;  
  10.     JPasswordField p1, p2;  
  11.     Registration()  
  12.     {  
  13.         setVisible(true);  
  14.         setSize(700700);  
  15.         setLayout(null);  
  16.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  17.         setTitle("Registration Form in Java");  
  18.         l1 = new JLabel("Registration Form in Windows Form:");  
  19.         l1.setForeground(Color.blue);  
  20.         l1.setFont(new Font("Serif", Font.BOLD, 20));  
  21.         l2 = new JLabel("Name:");  
  22.         l3 = new JLabel("Email-ID:");  
  23.         l4 = new JLabel("Create Passowrd:");  
  24.         l5 = new JLabel("Confirm Password:");  
  25.         l6 = new JLabel("Country:");  
  26.         l7 = new JLabel("State:");  
  27.         l8 = new JLabel("Phone No:");   
  28.         tf1 = new JTextField();  
  29.         tf2 = new JTextField();  
  30.         p1 = new JPasswordField();  
  31.         p2 = new JPasswordField();  
  32.         tf5 = new JTextField();  
  33.         tf6 = new JTextField();  
  34.         tf7 = new JTextField();  
  35.         btn1 = new JButton("Submit");  
  36.         btn2 = new JButton("Clear");  
  37.         btn1.addActionListener(this);  
  38.         btn2.addActionListener(this);  
  39.         l1.setBounds(1003040030);  
  40.         l2.setBounds(807020030);  
  41.         l3.setBounds(8011020030);  
  42.         l4.setBounds(8015020030);  
  43.         l5.setBounds(8019020030);  
  44.         l6.setBounds(8023020030);  
  45.         l7.setBounds(8027020030);  
  46.         l8.setBounds(8031020030);  
  47.         tf1.setBounds(3007020030);  
  48.         tf2.setBounds(30011020030);  
  49.         p1.setBounds(30015020030);  
  50.         p2.setBounds(30019020030);  
  51.         tf5.setBounds(30023020030);  
  52.         tf6.setBounds(30027020030);  
  53.         tf7.setBounds(30031020030);  
  54.         btn1.setBounds(5035010030);  
  55.         btn2.setBounds(17035010030);  
  56.         add(l1);  
  57.         add(l2);  
  58.         add(tf1);  
  59.         add(l3);  
  60.         add(tf2);  
  61.         add(l4);  
  62.         add(p1);  
  63.         add(l5);  
  64.         add(p2);  
  65.         add(l6);  
  66.         add(tf5);  
  67.         add(l7);  
  68.         add(tf6);  
  69.         add(l8);  
  70.         add(tf7);  
  71.         add(btn1);  
  72.         add(btn2);  
  73.     }  
  74.     public void actionPerformed(ActionEvent e)   
  75.     {  
  76.         if (e.getSource() == btn1)  
  77.          {  
  78.             int x = 0;  
  79.             String s1 = tf1.getText();  
  80.             String s2 = tf2.getText();  
  81.             char[] s3 = p1.getPassword();  
  82.             char[] s4 = p2.getPassword();   
  83.             String s8 = new String(s3);  
  84.             String s9 = new String(s4);  
  85.             String s5 = tf5.getText();  
  86.             String s6 = tf6.getText();  
  87.             String s7 = tf7.getText();  
  88.             if (s8.equals(s9))  
  89.             {  
  90.                 try  
  91.                 {  
  92.                     Class.forName("oracle.jdbc.driver.OracleDriver");  
  93.                     Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe""sandeep""welcome");  
  94.                     PreparedStatement ps = con.prepareStatement("insert into reg values(?,?,?,?,?,?)");  
  95.                     ps.setString(1, s1);  
  96.                     ps.setString(2, s2);  
  97.                     ps.setString(3, s8);  
  98.                     ps.setString(4, s5);  
  99.                     ps.setString(5, s6);  
  100.                     ps.setString(6, s7);  
  101.                     ResultSet rs = ps.executeQuery();  
  102.                     x++;  
  103.                     if (x > 0)   
  104.                     {  
  105.                         JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");  
  106.                     }  
  107.                 }  
  108.                 catch (Exception ex)   
  109.                 {  
  110.                     System.out.println(ex);  
  111.                 }  
  112.             }  
  113.             else  
  114.             {  
  115.                 JOptionPane.showMessageDialog(btn1, "Password Does Not Match");  
  116.             }   
  117.           }   
  118.           else  
  119.           {  
  120.             tf1.setText("");  
  121.             tf2.setText("");  
  122.             p1.setText("");  
  123.             p2.setText("");  
  124.             tf5.setText("");  
  125.             tf6.setText("");  
  126.             tf7.setText("");  
  127.           }  
  128.     }   
  129.     public static void main(String args[])  
  130.     {  
  131.         new Registration();  
  132.     }  
  133. }  
Note

You need to add a JAR file named "ojdbc.jar" to set up the database connection.

Step 5

Now our application is ready to run.

Right-click on the project menu then select "Run". The following output will be generated.

Output Generated

Step 6

Now we provide a password validation in this application so first check for that. For checking password validation we provide an incorrect password first, as in the following figure.

Filling Invalid Data

Password Not Match

Step 7

We have used a "clear" button so now check whether its working or not!

Output Fig

After clicking on the "clear" button the contents will be cleared as in the following.

After Click On Clear Button

Step 8

Now provide a correct entity and clicked on submit.

Form Filling

PopMenu Show Message

Step 9

If you want to be sure that the data was saved in the database

Then open the "Oracle Home Page" and login with your "username" and "password" then open "Object Explorer" then open the "reg" table; the following data exists over there.

reg.sql table


Similar Articles