Save student name and age using java swing

Java Swing is a GUI toolkit for Java that allows developers to create graphical user interfaces (GUIs) for desktop applications. To create a student management system using Java Swing, you would need to create the GUI components (e.g. buttons, text fields, labels, etc.) and implement the functionality to manage student data (e.g. adding, editing, and deleting students). You could use a database (e.g. MySQL) to store the student data and connect to it using Java's JDBC API. 

Example: 

  • Create a StudentManagementSystem Class

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class StudentManagementSystem {

    private JFrame mainFrame;
    private JLabel headerLabel;
    private JPanel controlPanel;
    private JTextField nameTextField;
    private JTextField ageTextField;
    private List<Student> students;

    public StudentManagementSystem() {
        prepareGUI();
        students = new ArrayList<>();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("Student Management System");
        mainFrame.setSize(600, 500);
        mainFrame.setLayout(new GridLayout(3, 1));

        headerLabel = new JLabel("", JLabel.CENTER);

        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.setVisible(true);
    }

    void showEventDemo() {
        headerLabel.setText("Add Student");

        JLabel nameLabel = new JLabel("Name: ", JLabel.RIGHT);
        JLabel ageLabel = new JLabel("Age: ", JLabel.RIGHT);
        nameTextField = new JTextField(10);
        ageTextField = new JTextField(10);

        JButton addButton = new JButton("Add");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isEmpty()) {

                    try {
                        String name = nameTextField.getText();
                        int age = Integer.parseInt(ageTextField.getText());
                        students.add(new Student(name, age));
                        JOptionPane.showMessageDialog(null, "Student added successfully");
                        clear();
                    } catch (NumberFormatException exception) {
                        JOptionPane.showMessageDialog(mainFrame, ageTextField.getText() + " is not a valid integer", "MESSAGE", JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });

        JButton displayButton = new JButton("Display");
        displayButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StringBuilder sb = new StringBuilder();
                for (Student student : students) {
                    sb.append(student).append("\n");
                }
                JOptionPane.showMessageDialog(mainFrame, sb.toString(), "Student List", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        controlPanel.add(nameLabel);
        controlPanel.add(nameTextField);
        controlPanel.add(ageLabel);
        controlPanel.add(ageTextField);
        controlPanel.add(addButton);
        controlPanel.add(displayButton);
        mainFrame.setVisible(true);
        mainFrame.setLocationRelativeTo(null);
    }

    private boolean isEmpty() {
        if (nameTextField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(mainFrame, "Name is required", "MESSAGE", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if (ageTextField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(mainFrame, "Age is required", "MESSAGE", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        return true;
    }

    private void clear() {
        nameTextField.setText("");
        ageTextField.setText("");
    }
}

  • Create a Student Class
package studentmanagementsystem;

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Name: " + name + ", Age: " + age;
    }
}

  • Create Main Class
package studentmanagementsystem;

public class Main {
    public static void main(String[] args) {
    StudentManagementSystem studentManagementSystem = new StudentManagementSystem();
    studentManagementSystem.showEventDemo();
}
}







To further enhance the functionality of this student management system, you could add the following features:
  • Store student data in a database (e.g. MySQL) instead of a text area.
  • Validate user inputs to ensure that only valid data is entered (e.g. name is not empty and age is a number).
  • Implement editing and deleting of student records.
  • Implement searching for student records based on name or age.
  • Add error handling for database connection and operations.
  • Implement security measures, such as password protection or user authentication.
  • Add a graphical chart to visualize the age distribution of students.
This is by no means a comprehensive list, but it gives you an idea of the types of features you can add to a Java Swing student management system.

Additionally, you can also consider the following best practices while developing the Java Swing Student Management System:
  • Use a layout manager (e.g. BorderLayout, GridLayout) to ensure consistent and flexible UI across different screen sizes.
  • Use appropriate components for data input (e.g. JTextField for short texts, JTextArea for longer texts).
  • Use meaningful variable names and organize your code into appropriate methods to make it more readable and maintainable.
  • Implement event-driven programming using listeners (e.g. ActionListener) to respond to user actions.
  • Test your code thoroughly to catch any bugs and to ensure the correct behavior of your application.
  • Use logging (e.g. java.util.logging) to keep track of important events and errors in your application.
  • Follow Java coding conventions and apply good software design principles (e.g. SOLID, DRY) to make your code more scalable and reusable.
By following these best practices, you can create a high-quality and reliable student management system using Java Swing.
Next Post Previous Post
No Comment
Add Comment
comment url