Create Customer Form using Java

Introduction

The Customer Form Application is a simple graphical user interface (GUI) application built in Java using Swing. It allows users to input customer details, such as name, customer number, age, and address. This project serves as an educational example for understanding basic GUI development in Java and handling form-based user inputs.

Prerequisites

Software Requirements:
  • Java Development Kit (JDK) 8 or above
  • IDE for development (e.g., Eclipse, IntelliJ IDEA, NetBeans)
  • Java Runtime Environment (JRE) 8 or above
Libraries/Dependencies:
  • No external libraries required (uses the standard Java Swing library).

Project Description

This project focuses on creating a form with four fields: Customer Name, Customer Number, Age, and Address. The form is displayed in a JFrame window, and the layout is managed using BorderLayout and GridLayout. The goal is to capture user input in a user-friendly interface.

Use Case:
This type of form can be used in applications such as customer databases, registration systems, or surveys.

Features

  • User-Friendly Interface: A simple and intuitive form layout.
  • Customer Information: Fields for Name, Customer Number, Age, and Address.
  • Resizable Window: The application window can be resized.
  • Cross-Platform: Runs on any system that supports Java (Windows, macOS, Linux).

Application Design

Class Structure:
  • CustomerForm: Main class of the application extending JFrame. It handles the layout and user interface components.
Components:
  1. JFrame: The main window.
  2. JLabel: Labels for identifying each field.
  3. JTextField: Input fields for user data entry.
  4. JPanel: A container to arrange the form components.
  5. SwingUtilities: Used to invoke the creation of the UI thread safely.

Code

				
					import javax.swing.*;
import java.awt.*;

public class CustomerForm extends JFrame {

    private JTextField nameField, numberField, ageField, addressField;

    public CustomerForm() {
        // Setting layout of the frame
        setLayout(new BorderLayout());

        // Heading Label at the top
        JLabel headingLabel = new JLabel("Customer Information", JLabel.CENTER);
        headingLabel.setFont(new Font("Arial", Font.BOLD, 24));
        headingLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));  // Add some padding
        add(headingLabel, BorderLayout.NORTH);

        // Creating form panel
        JPanel formPanel = new JPanel(new GridLayout(4, 2, 10, 10));  // 4 rows, 2 columns with 10px gaps

        // Customer Name
        JLabel nameLabel = new JLabel("Customer Name:");
        nameField = new JTextField();
        formPanel.add(nameLabel);
        formPanel.add(nameField);

        // Customer Number
        JLabel numberLabel = new JLabel("Customer Number:");
        numberField = new JTextField();
        formPanel.add(numberLabel);
        formPanel.add(numberField);

        // Age
        JLabel ageLabel = new JLabel("Age:");
        ageField = new JTextField();
        formPanel.add(ageLabel);
        formPanel.add(ageField);

        // Address
        JLabel addressLabel = new JLabel("Address:");
        addressField = new JTextField();
        formPanel.add(addressLabel);
        formPanel.add(addressField);

        // Add the form panel to the center
        add(formPanel, BorderLayout.CENTER);

        // Setting frame size and closing behavior
        setSize(400, 300);  // Width x Height
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // Close the application when the window is closed
    }

    public static void main(String[] args) {
        // Run the application
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CustomerForm frame = new CustomerForm();  // Create the frame
                frame.setVisible(true);  // Make the frame visible
            }
        });
    }
}

				
			

Code

				
					import javax.swing.*;
import java.awt.*;

public class CustomerForm extends JFrame {

    private JTextField nameField, numberField, ageField, addressField;

    public CustomerForm() {
        // Setting layout of the frame
        setLayout(new BorderLayout());

        // Heading Label at the top
        JLabel headingLabel = new JLabel("Customer Information", JLabel.CENTER);
        headingLabel.setFont(new Font("Arial", Font.BOLD, 24));
        headingLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));  // Add some padding
        add(headingLabel, BorderLayout.NORTH);

        // Creating form panel
        JPanel formPanel = new JPanel(new GridLayout(4, 2, 10, 10));  // 4 rows, 2 columns with 10px gaps

        // Customer Name
        JLabel nameLabel = new JLabel("Customer Name:");
        nameField = new JTextField();
        formPanel.add(nameLabel);
        formPanel.add(nameField);

        // Customer Number
        JLabel numberLabel = new JLabel("Customer Number:");
        numberField = new JTextField();
        formPanel.add(numberLabel);
        formPanel.add(numberField);

        // Age
        JLabel ageLabel = new JLabel("Age:");
        ageField = new JTextField();
        formPanel.add(ageLabel);
        formPanel.add(ageField);

        // Address
        JLabel addressLabel = new JLabel("Address:");
        addressField = new JTextField();
        formPanel.add(addressLabel);
        formPanel.add(addressField);

        // Add the form panel to the center
        add(formPanel, BorderLayout.CENTER);

        // Setting frame size and closing behavior
        setSize(400, 300);  // Width x Height
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // Close the application when the window is closed
    }

    public static void main(String[] args) {
        // Run the application
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CustomerForm frame = new CustomerForm();  // Create the frame
                frame.setVisible(true);  // Make the frame visible
            }
        });
    }
}

				
			

Code Explanation

  1. Using JFrame instead of JApplet:

    • We are now using JFrame, which is better suited for standalone applications.
    • The JFrame window is initialized in the constructor and made visible in the main() method.
  2. main() Method:

    • The main() method is necessary for any standalone Java application. It creates an instance of CustomerForm (a JFrame), sets its size, and makes it visible.
  3. Form Layout:

    • A GridLayout is used for the form with labels and text fields (Customer Name, Number, Age, and Address), while BorderLayout positions the form components in the frame.
  4. Closing Behavior:

    • The setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ensures the application exits when the frame is closed.

Running the Program:

Now, you can run this program as a standard Java application without the need for a browser or applet viewer. Simply compile and run it in your preferred IDE or terminal:

 

 

				
					javac CustomerForm.java
java CustomerForm
				
			

Testing

  • Input Validation: Currently, there is no input validation. To test, enter any values in the text fields and ensure they appear correctly.
  • Window Resizing: Verify that the form layout remains intact when resizing the window.
  • Cross-Platform Testing: The program has been tested on Windows. It should run on macOS and Linux environments as well.

Future Improvements

  • Input Validation: Add validation for fields such as customer number (should only accept numbers) and age (should be within a valid range).
  • Submit/Reset Buttons: Introduce buttons for submitting the form and clearing the input fields.
  • Data Storage: Store the customer details in a file or database for future use.
  • Look and Feel: Customize the Swing components to improve the UI aesthetics.

Conclusion

The Customer Form Application demonstrates the use of Java Swing to create a simple GUI form. It is a foundational project that showcases form input handling and basic layout management using JFrame and GridLayout. Future expansions can include more advanced features like form validation, data storage, and enhanced UI designs.

Project by Harshil Bansal, Team edSlash.