-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentInfoSystem.java
More file actions
739 lines (636 loc) · 30.6 KB
/
StudentInfoSystem.java
File metadata and controls
739 lines (636 loc) · 30.6 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class StudentInfoSystem extends JFrame {
// Components
private JTextField idField, nameField, phoneField, emailField, courseField, searchField;
private JComboBox<String> genderCombo, yearCombo, semesterCombo;
private JTextArea addressArea;
private JTable studentTable;
private DefaultTableModel tableModel;
private JButton addButton, updateButton, deleteButton, clearButton, searchButton;
private JLabel totalStudentsLabel;
// Data storage
private ArrayList<Student> students;
private int currentId = 1001;
// Reference to input panel for scrolling
private JPanel inputPanel;
// Student class
class Student {
String id;
String name;
String gender;
String phone;
String email;
String address;
String course;
String year;
String semester;
String registrationDate;
Student(String id, String name, String gender, String phone, String email,
String address, String course, String year, String semester) {
this.id = id;
this.name = name;
this.gender = gender;
this.phone = phone;
this.email = email;
this.address = address;
this.course = course;
this.year = year;
this.semester = semester;
this.registrationDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(new Date());
}
Object[] toArray() {
return new Object[]{id, name, gender, phone, email, address, course, year, semester, registrationDate};
}
}
public StudentInfoSystem() {
// Setup window
setTitle("Student Information System");
setSize(1200, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Initialize data
students = new ArrayList<>();
// Initialize UI
initComponents();
// Load sample data
loadSampleData();
}
private void initComponents() {
// Create custom colors
Color primaryColor = new Color(0, 123, 255); // Bootstrap primary blue
Color successColor = new Color(40, 167, 69); // Bootstrap success green
Color dangerColor = new Color(220, 53, 69); // Bootstrap danger red
Color warningColor = new Color(255, 193, 7); // Bootstrap warning yellow
Color infoColor = new Color(23, 162, 184); // Bootstrap info teal
Color backgroundColor = new Color(248, 249, 250); // Light gray background
Color panelColor = Color.WHITE;
// Set global font
Font labelFont = new Font("Arial", Font.BOLD, 14);
Font fieldFont = new Font("Arial", Font.PLAIN, 14);
Font titleFont = new Font("Arial", Font.BOLD, 16);
Font buttonFont = new Font("Arial", Font.BOLD, 14);
// Main container with background
Container container = getContentPane();
container.setLayout(new BorderLayout(0, 0));
container.setBackground(backgroundColor);
// Header Panel
JPanel headerPanel = new JPanel(new BorderLayout());
headerPanel.setBackground(primaryColor);
headerPanel.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));
JLabel titleLabel = new JLabel("STUDENT INFORMATION SYSTEM");
titleLabel.setFont(new Font("Arial", Font.BOLD, 28));
titleLabel.setForeground(Color.WHITE);
headerPanel.add(titleLabel, BorderLayout.WEST);
JLabel versionLabel = new JLabel("Version 1.0");
versionLabel.setFont(new Font("Arial", Font.ITALIC, 14));
versionLabel.setForeground(Color.WHITE);
headerPanel.add(versionLabel, BorderLayout.EAST);
container.add(headerPanel, BorderLayout.NORTH);
// Main Content Panel
JPanel mainContentPanel = new JPanel(new BorderLayout(10, 10));
mainContentPanel.setBackground(backgroundColor);
mainContentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Left Panel - Input Form with Scroll
JPanel leftPanel = new JPanel(new BorderLayout(10, 10));
leftPanel.setBackground(backgroundColor);
// Create a panel for the form that will be scrollable
JPanel formContentPanel = new JPanel();
formContentPanel.setLayout(new BorderLayout());
formContentPanel.setBackground(panelColor);
// Input Panel (This will be inside the scroll pane)
inputPanel = new JPanel();
inputPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(primaryColor, 2),
"Student Information",
TitledBorder.LEFT,
TitledBorder.TOP,
titleFont,
primaryColor
));
inputPanel.setBackground(panelColor);
inputPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 15, 10, 15);
gbc.fill = GridBagConstraints.HORIZONTAL;
// Row 0: Student ID
JLabel idLabel = new JLabel("Student ID:");
idLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(idLabel, gbc);
idField = new JTextField();
idField.setFont(fieldFont);
idField.setEditable(false);
idField.setBackground(new Color(240, 240, 240));
idField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(idField, gbc);
// Row 1: Full Name
JLabel nameLabel = new JLabel("Full Name:");
nameLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(nameLabel, gbc);
nameField = new JTextField();
nameField.setFont(fieldFont);
nameField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(nameField, gbc);
// Row 2: Gender
JLabel genderLabel = new JLabel("Gender:");
genderLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 2; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(genderLabel, gbc);
genderCombo = new JComboBox<>(new String[]{"Male", "Female", "Other"});
genderCombo.setFont(fieldFont);
genderCombo.setBackground(Color.WHITE);
genderCombo.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(genderCombo, gbc);
// Row 3: Phone
JLabel phoneLabel = new JLabel("Phone:");
phoneLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 3; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(phoneLabel, gbc);
phoneField = new JTextField();
phoneField.setFont(fieldFont);
phoneField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(phoneField, gbc);
// Row 4: Email
JLabel emailLabel = new JLabel("Email:");
emailLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 4; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(emailLabel, gbc);
emailField = new JTextField();
emailField.setFont(fieldFont);
emailField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(emailField, gbc);
// Row 5: Address
JLabel addressLabel = new JLabel("Address:");
addressLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 5; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(addressLabel, gbc);
addressArea = new JTextArea(4, 20);
addressArea.setFont(fieldFont);
addressArea.setLineWrap(true);
addressArea.setWrapStyleWord(true);
addressArea.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
JScrollPane addressScroll = new JScrollPane(addressArea);
addressScroll.setPreferredSize(new Dimension(250, 100));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(addressScroll, gbc);
// Row 6: Course
JLabel courseLabel = new JLabel("Course:");
courseLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 6; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(courseLabel, gbc);
courseField = new JTextField();
courseField.setFont(fieldFont);
courseField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(courseField, gbc);
// Row 7: Year
JLabel yearLabel = new JLabel("Year:");
yearLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 7; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(yearLabel, gbc);
yearCombo = new JComboBox<>(new String[]{"1st Year", "2nd Year", "3rd Year", "4th Year"});
yearCombo.setFont(fieldFont);
yearCombo.setBackground(Color.WHITE);
yearCombo.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(yearCombo, gbc);
// Row 8: Semester
JLabel semesterLabel = new JLabel("Semester:");
semesterLabel.setFont(labelFont);
gbc.gridx = 0; gbc.gridy = 8; gbc.anchor = GridBagConstraints.EAST;
gbc.weightx = 0.3;
inputPanel.add(semesterLabel, gbc);
semesterCombo = new JComboBox<>(new String[]{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th"});
semesterCombo.setFont(fieldFont);
semesterCombo.setBackground(Color.WHITE);
semesterCombo.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
gbc.gridx = 1; gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0.7;
inputPanel.add(semesterCombo, gbc);
// Add empty rows to make sure scrolling works
gbc.gridx = 0; gbc.gridy = 9; gbc.weighty = 1.0;
inputPanel.add(Box.createVerticalGlue(), gbc);
// Create scroll pane for the input panel
JScrollPane formScrollPane = new JScrollPane(inputPanel);
formScrollPane.setBorder(null);
formScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
formScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
formScrollPane.getVerticalScrollBar().setUnitIncrement(16); // Smooth scrolling
formContentPanel.add(formScrollPane, BorderLayout.CENTER);
// Button Panel (Fixed at bottom of left panel)
JPanel buttonPanel = new JPanel(new GridLayout(5, 1, 10, 10));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
buttonPanel.setBackground(panelColor);
// CHANGE 1: Button colors remain same but text color changed to BLACK
addButton = createStyledButton("Add Student", successColor, Color.BLACK);
updateButton = createStyledButton("Update Student", primaryColor, Color.BLACK);
deleteButton = createStyledButton("Delete Student", dangerColor, Color.BLACK);
clearButton = createStyledButton("Clear Fields", warningColor, Color.BLACK);
searchButton = createStyledButton("Search", infoColor, Color.BLACK);
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(clearButton);
buttonPanel.add(searchButton);
// Add components to left panel
leftPanel.add(formContentPanel, BorderLayout.CENTER);
leftPanel.add(buttonPanel, BorderLayout.SOUTH);
// Right Panel - Table
JPanel rightPanel = new JPanel(new BorderLayout(10, 10));
rightPanel.setBackground(backgroundColor);
// Search Panel
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
searchPanel.setBackground(panelColor);
searchPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(infoColor, 1),
"Search Student Records",
TitledBorder.LEFT,
TitledBorder.TOP,
new Font("Arial", Font.BOLD, 14),
infoColor
));
JLabel searchLabel = new JLabel("Search:");
searchLabel.setFont(labelFont);
searchPanel.add(searchLabel);
searchField = new JTextField(25);
searchField.setFont(fieldFont);
searchField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(8, 12, 8, 12)
));
searchPanel.add(searchField);
rightPanel.add(searchPanel, BorderLayout.NORTH);
// Table
String[] columns = {"ID", "Name", "Gender", "Phone", "Email", "Address", "Course", "Year", "Semester", "Reg Date"};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
studentTable = new JTable(tableModel);
studentTable.setFont(new Font("Arial", Font.PLAIN, 12));
studentTable.setRowHeight(30);
studentTable.setSelectionBackground(new Color(220, 237, 253));
studentTable.setSelectionForeground(Color.BLACK);
studentTable.setGridColor(new Color(240, 240, 240));
studentTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 13));
// CHANGE 2: Table header background remains same but text color changed to BLACK
studentTable.getTableHeader().setBackground(new Color(52, 152, 219));
studentTable.getTableHeader().setForeground(Color.BLACK); // Changed from WHITE to BLACK
studentTable.getTableHeader().setReorderingAllowed(false);
// Set column widths
studentTable.getColumnModel().getColumn(0).setPreferredWidth(80);
studentTable.getColumnModel().getColumn(1).setPreferredWidth(150);
studentTable.getColumnModel().getColumn(2).setPreferredWidth(70);
studentTable.getColumnModel().getColumn(3).setPreferredWidth(120);
studentTable.getColumnModel().getColumn(4).setPreferredWidth(180);
studentTable.getColumnModel().getColumn(5).setPreferredWidth(200);
studentTable.getColumnModel().getColumn(6).setPreferredWidth(150);
studentTable.getColumnModel().getColumn(7).setPreferredWidth(100);
studentTable.getColumnModel().getColumn(8).setPreferredWidth(80);
studentTable.getColumnModel().getColumn(9).setPreferredWidth(120);
JScrollPane tableScroll = new JScrollPane(studentTable);
tableScroll.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200)));
rightPanel.add(tableScroll, BorderLayout.CENTER);
// Status Panel
JPanel statusPanel = new JPanel(new BorderLayout());
statusPanel.setBackground(panelColor);
statusPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(10, 15, 10, 15)
));
totalStudentsLabel = new JLabel("Total Students: 0");
totalStudentsLabel.setFont(new Font("Arial", Font.BOLD, 14));
totalStudentsLabel.setForeground(primaryColor);
statusPanel.add(totalStudentsLabel, BorderLayout.WEST);
JLabel footerLabel = new JLabel("© 2025 Student Information System | Developed with Md Sabbir Ahmad", SwingConstants.RIGHT);
footerLabel.setFont(new Font("Arial", Font.ITALIC, 12));
footerLabel.setForeground(new Color(108, 117, 125));
statusPanel.add(footerLabel, BorderLayout.EAST);
rightPanel.add(statusPanel, BorderLayout.SOUTH);
// Add panels to main content panel
mainContentPanel.add(leftPanel, BorderLayout.WEST);
mainContentPanel.add(rightPanel, BorderLayout.CENTER);
// Add main content to container
container.add(mainContentPanel, BorderLayout.CENTER);
// Add action listeners
addButton.addActionListener(e -> addStudent());
updateButton.addActionListener(e -> updateStudent());
deleteButton.addActionListener(e -> deleteStudent());
clearButton.addActionListener(e -> clearFields());
searchButton.addActionListener(e -> searchStudents());
searchField.addActionListener(e -> searchStudents());
// Table selection listener
studentTable.getSelectionModel().addListSelectionListener(e -> {
if (!e.getValueIsAdjusting() && studentTable.getSelectedRow() != -1) {
loadSelectedStudent();
}
});
// Generate new ID
generateNewId();
}
// UPDATED METHOD: Now takes textColor as third parameter
private JButton createStyledButton(String text, Color backgroundColor, Color textColor) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 14));
button.setBackground(backgroundColor);
button.setForeground(textColor); // Changed from Color.WHITE to textColor parameter
button.setFocusPainted(false);
button.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(backgroundColor.darker(), 2),
BorderFactory.createEmptyBorder(12, 25, 12, 25)
));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Hover effect
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
button.setBackground(backgroundColor.brighter());
}
@Override
public void mouseExited(MouseEvent e) {
button.setBackground(backgroundColor);
}
});
return button;
}
private void loadSampleData() {
// Add some sample data
String[][] sampleData = {
// {"S1001", "John Smith", "Male", "01712345678", "john@edu.com", "123 Main St, Dhaka", "Computer Science", "3rd Year", "5th"},
// {"S1002", "Sarah Johnson", "Female", "01898765432", "sarah@edu.com", "456 Park Ave, Chittagong", "Electrical Engineering", "2nd Year", "3rd"},
// {"S1003", "David Wilson", "Male", "01911223344", "david@edu.com", "789 Oak Rd, Sylhet", "Business Administration", "4th Year", "7th"},
// {"S1004", "Lisa Brown", "Female", "01655667788", "lisa@edu.com", "321 Pine St, Khulna", "Mechanical Engineering", "1st Year", "2nd"},
// {"S1005", "Michael Davis", "Male", "01566778899", "michael@edu.com", "654 Elm Ave, Rajshahi", "Civil Engineering", "3rd Year", "6th"},
// {"S1006", "Emma Wilson", "Female", "01744556677", "emma@edu.com", "987 Maple St, Dhaka", "Software Engineering", "2nd Year", "4th"},
// {"S1007", "James Miller", "Male", "01933445566", "james@edu.com", "654 Oak Ave, Chittagong", "Data Science", "3rd Year", "5th"},
// {"S1008", "Sophia Brown", "Female", "01877889900", "sophia@edu.com", "321 Cedar Rd, Sylhet", "Cyber Security", "4th Year", "8th"},
// {"S1009", "William Taylor", "Male", "01611223344", "william@edu.com", "789 Pine St, Khulna", "Artificial Intelligence", "1st Year", "1st"},
// {"S1010", "Olivia Anderson", "Female", "01555667788", "olivia@edu.com", "456 Birch Ave, Rajshahi", "Computer Engineering", "2nd Year", "3rd"}
};
for (String[] data : sampleData) {
Student student = new Student(data[0], data[1], data[2], data[3], data[4],
data[5], data[6], data[7], data[8]);
students.add(student);
tableModel.addRow(student.toArray());
}
updateTotalStudents();
currentId = 1001; // Set next ID
generateNewId();
}
private void generateNewId() {
idField.setText("S" + currentId);
}
private void addStudent() {
if (validateInputs()) {
String id = idField.getText();
String name = nameField.getText();
String gender = (String) genderCombo.getSelectedItem();
String phone = phoneField.getText();
String email = emailField.getText();
String address = addressArea.getText();
String course = courseField.getText();
String year = (String) yearCombo.getSelectedItem();
String semester = (String) semesterCombo.getSelectedItem();
Student student = new Student(id, name, gender, phone, email, address, course, year, semester);
students.add(student);
tableModel.addRow(student.toArray());
currentId++;
generateNewId();
clearFields();
updateTotalStudents();
JOptionPane.showMessageDialog(this, "✅ Student added successfully!",
"Success", JOptionPane.INFORMATION_MESSAGE);
}
}
private void updateStudent() {
int selectedRow = studentTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "⚠️ Please select a student to update",
"No Selection", JOptionPane.WARNING_MESSAGE);
return;
}
if (validateInputs()) {
String id = idField.getText();
String name = nameField.getText();
String gender = (String) genderCombo.getSelectedItem();
String phone = phoneField.getText();
String email = emailField.getText();
String address = addressArea.getText();
String course = courseField.getText();
String year = (String) yearCombo.getSelectedItem();
String semester = (String) semesterCombo.getSelectedItem();
// Update in ArrayList
Student student = students.get(selectedRow);
student.name = name;
student.gender = gender;
student.phone = phone;
student.email = email;
student.address = address;
student.course = course;
student.year = year;
student.semester = semester;
// Update in table
tableModel.setValueAt(name, selectedRow, 1);
tableModel.setValueAt(gender, selectedRow, 2);
tableModel.setValueAt(phone, selectedRow, 3);
tableModel.setValueAt(email, selectedRow, 4);
tableModel.setValueAt(address, selectedRow, 5);
tableModel.setValueAt(course, selectedRow, 6);
tableModel.setValueAt(year, selectedRow, 7);
tableModel.setValueAt(semester, selectedRow, 8);
JOptionPane.showMessageDialog(this, "✅ Student updated successfully!",
"Success", JOptionPane.INFORMATION_MESSAGE);
}
}
private void deleteStudent() {
int selectedRow = studentTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "⚠️ Please select a student to delete",
"No Selection", JOptionPane.WARNING_MESSAGE);
return;
}
int confirm = JOptionPane.showConfirmDialog(this,
"🗑️ Are you sure you want to delete this student?",
"Confirm Delete", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
students.remove(selectedRow);
tableModel.removeRow(selectedRow);
clearFields();
updateTotalStudents();
JOptionPane.showMessageDialog(this, "✅ Student deleted successfully!",
"Success", JOptionPane.INFORMATION_MESSAGE);
}
}
private void searchStudents() {
String searchText = searchField.getText().toLowerCase();
if (searchText.isEmpty()) {
// Show all students
tableModel.setRowCount(0);
for (Student student : students) {
tableModel.addRow(student.toArray());
}
return;
}
// Filter students
tableModel.setRowCount(0);
for (Student student : students) {
if (student.id.toLowerCase().contains(searchText) ||
student.name.toLowerCase().contains(searchText) ||
student.phone.contains(searchText) ||
student.email.toLowerCase().contains(searchText) ||
student.course.toLowerCase().contains(searchText)) {
tableModel.addRow(student.toArray());
}
}
}
private void loadSelectedStudent() {
int selectedRow = studentTable.getSelectedRow();
if (selectedRow != -1) {
Student student = students.get(selectedRow);
idField.setText(student.id);
nameField.setText(student.name);
genderCombo.setSelectedItem(student.gender);
phoneField.setText(student.phone);
emailField.setText(student.email);
addressArea.setText(student.address);
courseField.setText(student.course);
yearCombo.setSelectedItem(student.year);
semesterCombo.setSelectedItem(student.semester);
// Scroll to top of form
scrollToTop();
}
}
private void clearFields() {
generateNewId();
nameField.setText("");
genderCombo.setSelectedIndex(0);
phoneField.setText("");
emailField.setText("");
addressArea.setText("");
courseField.setText("");
yearCombo.setSelectedIndex(0);
semesterCombo.setSelectedIndex(0);
studentTable.clearSelection();
searchField.setText("");
searchStudents(); // Reset search
nameField.requestFocus();
// Scroll to top of form
scrollToTop();
}
private void scrollToTop() {
SwingUtilities.invokeLater(() -> {
Container parent = inputPanel.getParent();
if (parent instanceof JViewport) {
((JViewport) parent).setViewPosition(new Point(0, 0));
}
});
}
private boolean validateInputs() {
if (nameField.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "❌ Please enter student name",
"Validation Error", JOptionPane.ERROR_MESSAGE);
nameField.requestFocus();
return false;
}
if (phoneField.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "❌ Please enter phone number",
"Validation Error", JOptionPane.ERROR_MESSAGE);
phoneField.requestFocus();
return false;
}
if (emailField.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "❌ Please enter email address",
"Validation Error", JOptionPane.ERROR_MESSAGE);
emailField.requestFocus();
return false;
}
if (courseField.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(this, "❌ Please enter course name",
"Validation Error", JOptionPane.ERROR_MESSAGE);
courseField.requestFocus();
return false;
}
// Email validation
String email = emailField.getText().trim();
if (!email.contains("@") || !email.contains(".")) {
JOptionPane.showMessageDialog(this, "❌ Please enter a valid email address",
"Validation Error", JOptionPane.ERROR_MESSAGE);
emailField.requestFocus();
return false;
}
return true;
}
private void updateTotalStudents() {
totalStudentsLabel.setText("Total Students: " + students.size());
}
public static void main(String[] args) {
// Set look and feel for better appearance
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// Run the application
SwingUtilities.invokeLater(() -> {
StudentInfoSystem system = new StudentInfoSystem();
system.setVisible(true);
});
}
}