Introduction of Swing:

java.swing Package:  Swing is a GUI widget toolkit for java. Swing provides a native look and feel that emulates the look and feel of several platforms. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists.

java.awt.event Package: In this package we use ActionListener Interface. The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.
java.awt.GridLayout Package: The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle, like rows and columns.
java.util Package: Java.util package contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes.

Swing is the principal of GUI toolkit for the Java programming language. It is a part of the JFC (Java Foundation Classes), which is an API for providing a graphical user interface for Java programs.Graphical Usert Interface (GUI) for registartion is created in this code using the swing package. It is completely written in Java.

Swing is a collection of packages for creating full featured desktop applications. JFC consists of AWT, Swing, and Accessibility, Java 2D, and Drag and Drop. Swing was released in 1997 with JDK 1.2. It is a mature toolkit.

In our program we will not use only Swing package some other packages are included, first we will discuss why these packages are necessary for our program:

  •     javax.swing;
  •     java.awt.event;
  •     java.util.*;
  •     java.awt.GridLayout; 


Java Codes:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.event.*;
import java.util.*;
import java.awt.GridLayout;

public class RegistrationForm extends JFrame implements ActionListener{   JLabel title, idLabel, nameLabel, genderLabel, addressLabel, contactLabel;   JTextField idField, nameField, genderField, addressField, contactField;   JButton registerButton, exitButton;   JRadioButton male, female;   ButtonGroup bg;   JPanel panel;   List<User> list = new ArrayList<User>();   JTable table;   String gender = "";   // Returns a column class of Object   DefaultTableModel model;   JScrollPane scrollpane;   Object[][] data;   // Defining Constructor   RegistrationForm() {   setSize(700, 360);   setLayout(null);     //Defining Labels   title = new JLabel("Registration Form");   title.setBounds(60, 7, 200, 30);   idLabel = new JLabel("ID");  idLabel.setBounds(30, 50, 60, 30);   nameLabel = new JLabel("Name");   nameLabel.setBounds(30, 85, 60, 30);   genderLabel =new JLabel("Gender");   genderLabel.setBounds(30, 120, 60, 30);   addressLabel = new JLabel("Address");   addressLabel.setBounds(30, 155, 60, 30);   contactLabel = new JLabel("Contact");   contactLabel.setBounds(30, 190, 60, 30);   // Defining ID field   idField = new JTextField();   idField.setBounds(95, 50, 100, 30);   idField.addKeyListener(new KeyAdapter() {   public void keyTyped(KeyEvent e) {   char c = e.getKeyChar();   if(!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) ||   (c == KeyEvent.VK_DELETE))) {   e.consume();  
}   }  
});   // Defining Name field   nameField = new JTextField();   nameField.setBounds(95, 85, 100, 30);
  // Defining Gender Buttons  male = new JRadioButton("Male");   male.setBounds(95, 120, 60, 30);   male.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {      gender= "Male";
    female = new JRadioButton("Female");  female.setBounds(155,120, 70, 30);  
female.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
      gender ="Female";
bg = new ButtonGroup();
     addressField = new JTextField();
   addressField.setBounds(95, 155, 100, 30);
contactField = new JTextField();  
contactField.setBounds(95, 190, 100, 30);   contactField.addKeyListener(new KeyAdapter() {      public void keyTyped(KeyEvent e) {
   char c = e.getKeyChar();   if(!((c >= '0') && (c <= '9') || (c ==KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) {   e.consume();
   //Defining Exit Button      exitButton = new JButton("Exit");   exitButton.setBounds(25, 230, 80, 30);   exitButton.addActionListener(this);     //Defining Register Button    registerButton = new JButton("Register");   registerButton.setBounds(110, 230, 100, 30);   registerButton.addActionListener(this);     // fixing all Label,TextField,Button         add(title);        add(idLabel);       add(nameLabel);     add(genderLabel);      add(addressLabel);       add(contactLabel);        add(idField);        add(nameField);         add(male);         add(female);        add(addressField);       add(contactField);      add(exitButton);     add(registerButton);       //Defining Panel      panel =new JPanel();      panel.setLayout(new GridLayout());      panel.setBounds(250,10, 400, 300);   panel.setBorder(BorderFactory.createDashedBorder(;     add(panel);      // Defining Model for table        model = new DefaultTableModel();         table = new Table(model);          table.setEnabled(false);    // Defining Column Names on model           model.addColumn("ID");           model.addColumn("Name");           model.addColumn("Gender");           model.addColumn("Address");          model.addColumn("Contact");      // Enable Scrolling on table       scrollpane = new jScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,  
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);panel.add(scrollpane);   panel.setEnabled(false);   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setResizable(false);         setVisible(true);
    public void actionPerformed(ActionEvent ae) {
    if(ae.getSource() == exitButton) {
    if(ae.getSource() ==registerButton) {  
if(idField.getText().equals("") || nameField.getText().equals("") ||   addressField.getText().equals("")|| contactField.getText().equals("")||   gender.equals(""))JOptionPane.showMessageDialog(idField, "Fields will not be blank"); 
     //Storing records in List        list.add(new User(idField.getText(), nameField.getText(), gender,   addressField.getText(), contactField.getText()));         addRows();        // using for DialogBox         JOptionPane.showMessageDialog(this,"Successfully Registered");          idField.setText("");         nameField.setText("");        gender ="";    bg.clearSelection();   addressField.setText("");   contactField.setText("");
    //Adding records in List
      public void addRows() { 
         Object[] row =null;
     User str = list.get(list.size()- 1); 
      String string =str.uid + "," + str.uname + "," + str.gender +"," + str.uaddress + "," +   str.ucontact;
             row =string.split(",");
       //Adding records in table model   model.addRow(row);   panel.revalidate(); 
        public static void main(String[] args) { 
           new RegistrationForm(); 
Uer Class –
public class User
           String uid;
           String uname;
           String gender;
           String uaddress;
           String ucontact;
           User(String id,String name,String gender,String address,String contact) {
                  this.uid = id;
                  this.uname = name;
                  this.gender = gender;
                  this.uaddress = address;
                  this.ucontact = contact;

Running the Application

Next, we will learn about : Simple Registration Form using JAVA Swing - Step2

(Connecting GUI with MySQL Database using JDBC) 

  Modified On May-07-2018 03:31:18 AM

Leave Comment