NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package course_registration;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;

enum Course {
C("C"),
CPP("C++"),
JAVA("Java"),
PYTHON("Python"),
PHP("PHP"),
PERL("Perl"),
JAVASCRIPT("JavaScript"),
HTML("HTML"),
CSS("CSS");

private final String displayName;

Course(String displayName) {
this.displayName = displayName;
}

@Override
public String toString() {
return displayName;
}
}

class Student implements Serializable {

private String studentID;
private String studentName;
private String mobileNo;
private Course course;
private String gender;

public Student(String studentID, String studentName, String mobileNo, Course course, String gender) {
this.studentID = studentID;
this.studentName = studentName;
this.mobileNo = mobileNo;
this.course = course;
this.gender = gender;
}

public String getStudentID() {
return studentID;
}

public String getStudentName() {
return studentName;
}

public String getMobileNo() {
return mobileNo;
}

public Course getCourse() {
return course;
}

public String getGender() {
return gender;
}
}

public class StudentRegistration extends JFrame {

private JTextField studentIDField;
private JTextField studentNameField;
private JTextField mobileNoField;
private JComboBox<Course> courseComboBox;
private JComboBox<String> genderComboBox;
private JTextField searchField;
private JTextField deleteField;
private DefaultTableModel tableModel;

public StudentRegistration() {
setTitle("Programming Course");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);

// Main Panel
JPanel mainPanel = new JPanel(new BorderLayout());

// Header
JPanel headerPanel = new JPanel();
headerPanel.setBackground(new Color(76, 175, 80));
headerPanel.setPreferredSize(new Dimension(800, 60));
JLabel titleLabel = new JLabel("Programming Course");
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
titleLabel.setForeground(Color.WHITE);
JLabel subtitleLabel = new JLabel("Student Registration");
subtitleLabel.setFont(new Font("Arial", Font.PLAIN, 16));
subtitleLabel.setForeground(Color.WHITE);
headerPanel.setLayout(new BoxLayout(headerPanel, BoxLayout.Y_AXIS));
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
subtitleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
headerPanel.add(titleLabel);
headerPanel.add(subtitleLabel);

// Registration Panel
JPanel registrationPanel = new JPanel();
registrationPanel.setLayout(new GridBagLayout());
registrationPanel.setBorder(BorderFactory.createTitledBorder("Registration"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 5, 5, 5); // Reduced top padding
gbc.anchor = GridBagConstraints.WEST;

studentIDField = new JTextField(15);
studentNameField = new JTextField(15);
mobileNoField = new JTextField(15);
courseComboBox = new JComboBox<>(Course.values());
genderComboBox = new JComboBox<>(new String[]{"Male", "Female"});
searchField = new JTextField(15);
deleteField = new JTextField(15);

gbc.gridx = 0;
gbc.gridy = 0;
registrationPanel.add(new JLabel("Student ID"), gbc);
gbc.gridx = 1;
registrationPanel.add(studentIDField, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
registrationPanel.add(new JLabel("Student Name"), gbc);
gbc.gridx = 1;
registrationPanel.add(studentNameField, gbc);

gbc.gridx = 0;
gbc.gridy = 2;
registrationPanel.add(new JLabel("Mobile No"), gbc);
gbc.gridx = 1;
registrationPanel.add(mobileNoField, gbc);

gbc.gridx = 0;
gbc.gridy = 3;
registrationPanel.add(new JLabel("Course"), gbc);
gbc.gridx = 1;
registrationPanel.add(courseComboBox, gbc);

gbc.gridx = 0;
gbc.gridy = 4;
registrationPanel.add(new JLabel("Gender"), gbc);
gbc.gridx = 1;
registrationPanel.add(genderComboBox, gbc);

JButton addButton = new JButton("Add");
JButton readButton = new JButton("Read");

gbc.gridx = 0;
gbc.gridy = 5;
registrationPanel.add(addButton, gbc);
gbc.gridx = 1;
registrationPanel.add(readButton, gbc);

gbc.gridx = 0;
gbc.gridy = 6;
registrationPanel.add(new JLabel("Search by ID"), gbc);
gbc.gridx = 1;
registrationPanel.add(searchField, gbc);

JButton searchButton = new JButton("Search");
gbc.gridx = 1;
gbc.gridy = 7;
registrationPanel.add(searchButton, gbc);

gbc.gridx = 0;
gbc.gridy = 8;
registrationPanel.add(new JLabel("Delete by ID"), gbc);
gbc.gridx = 1;
registrationPanel.add(deleteField, gbc);

JButton deleteButton = new JButton("Delete");
deleteButton.setBackground(Color.RED);
deleteButton.setForeground(Color.WHITE);
gbc.gridx = 1;
gbc.gridy = 9;
registrationPanel.add(deleteButton, gbc);

// Table Panel
JPanel tablePanel = new JPanel();
tablePanel.setBorder(BorderFactory.createTitledBorder("Student List"));
tableModel = new DefaultTableModel(new String[]{"Id", "Name", "Mobile", "Course", "Gender"}, 0);
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
tablePanel.setLayout(new BorderLayout());
tablePanel.add(scrollPane, BorderLayout.CENTER);

mainPanel.add(headerPanel, BorderLayout.NORTH);
mainPanel.add(registrationPanel, BorderLayout.WEST);
mainPanel.add(tablePanel, BorderLayout.CENTER);

add(mainPanel);

// Event Listeners
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String studentID = studentIDField.getText();
String studentName = studentNameField.getText();
String mobileNo = mobileNoField.getText();
Course course = (Course) courseComboBox.getSelectedItem();
String gender = (String) genderComboBox.getSelectedItem();

if (!studentID.isEmpty() && !studentName.isEmpty() && !mobileNo.isEmpty()) {
Student student = new Student(studentID, studentName, mobileNo, course, gender);
saveStudent(student);

studentIDField.setText("");
studentNameField.setText("");
mobileNoField.setText("");
courseComboBox.setSelectedIndex(0);
genderComboBox.setSelectedIndex(0);
} else {
JOptionPane.showMessageDialog(null, "Please fill in all fields.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

readButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
readStudents();
}
});

searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String searchID = searchField.getText();
if (!searchID.isEmpty()) {
searchStudent(searchID);
} else {
JOptionPane.showMessageDialog(null, "Please enter a Student ID to search.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String deleteID = deleteField.getText();
if (!deleteID.isEmpty()) {
deleteStudent(deleteID);
deleteField.setText("");
} else {
JOptionPane.showMessageDialog(null, "Please enter a Student ID to delete.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
}

private void saveStudent(Student student) {
ArrayList<Student> studentList = new ArrayList<>();
File file = new File("students.ser");
if (file.exists()) {
studentList = readStudentList();
}
studentList.add(student);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
oos.writeObject(studentList);
} catch (IOException e) {
e.printStackTrace();
}
}

private ArrayList<Student> readStudentList() {
ArrayList<Student> studentList = new ArrayList<>();
File file = new File("students.ser");
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
studentList = (ArrayList<Student>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
return studentList;
}

private void readStudents() {
tableModel.setRowCount(0);
ArrayList<Student> studentList = readStudentList();
for (Student student : studentList) {
tableModel.addRow(new Object[]{
student.getStudentID(),
student.getStudentName(),
student.getMobileNo(),
student.getCourse().toString(),
student.getGender()
});
}
}

private void searchStudent(String studentID) {
tableModel.setRowCount(0);
ArrayList<Student> studentList = readStudentList();
boolean found = false;
for (Student student : studentList) {
if (student.getStudentID().equals(studentID)) {
tableModel.addRow(new Object[]{
student.getStudentID(),
student.getStudentName(),
student.getMobileNo(),
student.getCourse().toString(),
student.getGender()
});
found = true;
}
}
if (!found) {
JOptionPane.showMessageDialog(null, "No match found", "Information", JOptionPane.INFORMATION_MESSAGE);
}
}

private void deleteStudent(String studentID) {
ArrayList<Student> studentList = readStudentList();
boolean found = false;
for (Student student : studentList) {
if (student.getStudentID().equals(studentID)) {
studentList.remove(student);
found = true;
break;
}
}
if (found) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.ser"))) {
oos.writeObject(studentList);
JOptionPane.showMessageDialog(null, "Student deleted successfully.", "Information", JOptionPane.INFORMATION_MESSAGE);
readStudents();
} catch (IOException e) {
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "No match found", "Information", JOptionPane.INFORMATION_MESSAGE);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new StudentRegistration().setVisible(true);
}
});
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.