-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentInformationSystem.java
166 lines (140 loc) · 5.59 KB
/
StudentInformationSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
// Class representing a Student
class Student {
private String name;
private String id;
private String email;
public Student(String name, String id, String email) {
this.name = name;
this.id = id;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Email: " + email;
}
}
// Main class for the Student Information System with GUI
public class StudentInformationSystem {
private static ArrayList<Student> studentList = new ArrayList<>();
private static DefaultListModel<String> listModel = new DefaultListModel<>();
private static JList<String> studentJList = new JList<>(listModel);
public static void main(String[] args) {
// Create the main JFrame
JFrame frame = new JFrame("Student Information System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
// Panel for input fields and buttons
JPanel panel = new JPanel(new GridLayout(5, 2));
// Input fields for student information
JTextField nameField = new JTextField();
JTextField idField = new JTextField();
JTextField emailField = new JTextField();
// Add labels and text fields to panel
panel.add(new JLabel("Name:"));
panel.add(nameField);
panel.add(new JLabel("ID:"));
panel.add(idField);
panel.add(new JLabel("Email:"));
panel.add(emailField);
// Buttons for CRUD operations
JButton addButton = new JButton("Add");
JButton updateButton = new JButton("Update");
JButton deleteButton = new JButton("Delete");
JButton viewButton = new JButton("View");
// Add buttons to the panel
panel.add(addButton);
panel.add(updateButton);
panel.add(deleteButton);
panel.add(viewButton);
// Student List Display Area
JScrollPane scrollPane = new JScrollPane(studentJList);
// Add event listeners for buttons
addButton.addActionListener(e -> {
String name = nameField.getText();
String id = idField.getText();
String email = emailField.getText();
if (!name.isEmpty() && !id.isEmpty() && !email.isEmpty()) {
Student student = new Student(name, id, email);
studentList.add(student);
listModel.addElement(student.toString());
clearFields(nameField, idField, emailField);
} else {
JOptionPane.showMessageDialog(frame, "All fields must be filled!", "Error", JOptionPane.ERROR_MESSAGE);
}
});
updateButton.addActionListener(e -> {
int selectedIndex = studentJList.getSelectedIndex();
if (selectedIndex != -1) {
String name = nameField.getText();
String id = idField.getText();
String email = emailField.getText();
if (!name.isEmpty() && !id.isEmpty() && !email.isEmpty()) {
Student student = studentList.get(selectedIndex);
student.setName(name);
student.setId(id);
student.setEmail(email);
listModel.set(selectedIndex, student.toString());
clearFields(nameField, idField, emailField);
} else {
JOptionPane.showMessageDialog(frame, "All fields must be filled!", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(frame, "Please select a student to update!", "Error", JOptionPane.ERROR_MESSAGE);
}
});
deleteButton.addActionListener(e -> {
int selectedIndex = studentJList.getSelectedIndex();
if (selectedIndex != -1) {
studentList.remove(selectedIndex);
listModel.remove(selectedIndex);
} else {
JOptionPane.showMessageDialog(frame, "Please select a student to delete!", "Error", JOptionPane.ERROR_MESSAGE);
}
});
viewButton.addActionListener(e -> {
int selectedIndex = studentJList.getSelectedIndex();
if (selectedIndex != -1) {
Student student = studentList.get(selectedIndex);
nameField.setText(student.getName());
idField.setText(student.getId());
emailField.setText(student.getEmail());
} else {
JOptionPane.showMessageDialog(frame, "Please select a student to view!", "Error", JOptionPane.ERROR_MESSAGE);
}
});
// Set layout and add components to the frame
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
// Display the frame
frame.setVisible(true);
}
// Helper method to clear text fields
private static void clearFields(JTextField nameField, JTextField idField, JTextField emailField) {
nameField.setText("");
idField.setText("");
emailField.setText("");
}
}