Student Management System (JavaFX Application)
Student Management System (JavaFX Application)
java
1 import java.io.BufferedReader;
2 import java.io.FileNotFoundException;
3 import java.io.FileReader;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.Optional;
8 import javafx.application.Application;
9 import javafx.application.Platform;
10 import javafx.beans.property.DoubleProperty;
11 import javafx.beans.property.SimpleDoubleProperty;
12 import javafx.beans.property.SimpleStringProperty;
13 import javafx.beans.property.StringProperty;
14 import javafx.collections.FXCollections;
15 import javafx.collections.ObservableList;
16 import javafx.geometry.Insets;
17 import javafx.geometry.Pos;
18 import javafx.scene.Scene;
19 import javafx.scene.control.*;
20 import javafx.scene.layout.*;
21 import javafx.stage.Modality;
22 import javafx.stage.Stage;
23
25
29
30 // File path for the CSV file
32
34 launch(args);
35 }
36
37 @Override
39 studentList = readDataFromFile();
41
45
48
51
52 tableView.getColumns().add(nameColumn);
53 tableView.getColumns().add(idColumn);
54 tableView.getColumns().add(courseColumn);
55 tableView.setItems(studentList);
56
61
66
67 {
68 detailsButton.setOnAction(event -> {
70 showStudentDetails(student);
71 });
72 }
73
74 @Override
76 super.updateItem(item, empty);
77 if (empty) {
78 setGraphic(null);
79 } else {
80 setGraphic(detailsButton);
81 }
82 }
83 });
84 tableView.getColumns().add(detailsColumn);
85
89
90 // Save Data Button
92 saveButton.setOnAction(e -> {
95 progressStage.initModality(Modality.APPLICATION_MODAL);
96 progressStage.setTitle("Saving Data");
97
10 progressBox.setAlignment(Pos.CENTER);
0
progressBox.setPadding(new Insets(20));
10
1
Scene progressScene = new Scene(progressBox);
10
2 progressStage.setScene(progressScene);
10 progressStage.show();
3
10
4 // Run the save operation in a separate thread
10
8 // Show the confirmation alert
10 Alert alert = new Alert(Alert.AlertType.INFORMATION);
9
alert.setTitle("Save Successful");
11
0 alert.setHeaderText(null);
11
5 // Show error alert
11 Alert errorAlert = new Alert(Alert.AlertType.ERROR);
6
errorAlert.setTitle("Error");
11
7 errorAlert.setHeaderText("Save Failed");
11 errorAlert.showAndWait();
9 });
12 }
0
}).start();
12
1 });
12
2
12
3
// Button Layout
12
4 HBox buttonLayout = new HBox(10);
12 buttonLayout.setPadding(new Insets(10));
5
buttonLayout.getChildren().addAll(addStudentButton, saveButton);
12
6
12
7 // Populate the course ComboBox
12
// Layout for input fields and buttons
9
GridPane inputGrid = new GridPane();
13
0 inputGrid.setPadding(new Insets(10, 10, 10, 10));
13 inputGrid.setVgap(10);
1
inputGrid.setHgap(10);
13
2
// Main layout
13
3 BorderPane borderPane = new BorderPane();
13 borderPane.setCenter(tableView);
4
borderPane.setBottom(buttonLayout);
13
5
13 primaryStage.setOnCloseRequest(event -> {
9 if (isDataDifferent()) {
14 Alert confirmDialog = new Alert(Alert.AlertType.CONFIRMATION);
0
confirmDialog.setTitle("Unsaved Changes");
14
1 confirmDialog.setHeaderText("Save Changes");
14
Optional<ButtonType> result = confirmDialog.showAndWait();
3
if (result.isPresent() && result.get() == ButtonType.OK) {
14
4 try {
14 writeDataToFile(studentList);
5
} catch (IOException ex) {
14
ex.printStackTrace(); // Handle save error
6
}
14
7 }
14 }
8
});
14
9
15
0
Scene scene = new Scene(borderPane, 600, 400);
15
primaryStage.setScene(scene);
1
primaryStage.show();
15
2 }
15
3
private boolean isDataDifferent() {
15
4 try {
15 return true; // Assume data is different if there's an error reading the file
8 }
15 }
9
16
0
16
3 TextField nameField = new TextField();
16 nameField.setPromptText("Name");
4
TextField idField = new TextField();
16
idField.setPromptText("ID");
5
ComboBox<String> courseComboBox = new ComboBox<>();
16
6 courseComboBox.getItems().addAll("Math", "Science", "History", "English");
16 courseComboBox.setPromptText("Course");
7
16
8
16 Button addButton = new Button("Add");
9
addButton.setOnAction(e -> {
17
String name = nameField.getText();
0
String id = idField.getText();
17
1 String course = courseComboBox.getValue();
17 try {
2
Student newStudent = new Student(name, id, course);
17
3 studentList.add(newStudent);
17 tableView.refresh();
4 addStudentStage.close();
17 } catch (NumberFormatException ex) {
5
System.out.println("Invalid grade input"); // Replace with error handling logic
17
6 }
17 });
7
18
3
addStudentStage.setScene(new Scene(formLayout, 300, 300));
18
addStudentStage.show();
4
}
18
5
18
TextField nameField = new TextField(student.getName());
9
TextField idField = new TextField(student.getId());
19
0
19
4 TextField gradeField = new TextField(String.valueOf(student.getGrade()));
19
5
Button updateButton = new Button("Update");
19
6 updateButton.setOnAction(e -> {
20
5 // Delete Button
20 Button deleteButton = new Button("Delete");
6
20 deleteButton.setOnAction(e -> {
7
Alert confirmDialog = new Alert(Alert.AlertType.CONFIRMATION);
20
confirmDialog.setTitle("Confirm Deletion");
8
confirmDialog.setHeaderText("Delete Student Record");
20
9 confirmDialog.setContentText("Delete " + student.getName() + " from record?");
21
0
// Optional: Customizing the buttons
21
1 ButtonType buttonTypeYes = new ButtonType("Yes", ButtonBar.ButtonData.YES);
21 confirmDialog.getButtonTypes().setAll(buttonTypeYes, buttonTypeCancel);
3
21 tableView.refresh();
6 detailsStage.close();
21 }
7
});
21
8
22
1 // Layout for the form
22
8 Scene scene = new Scene(layout, 300, 350);
22 detailsStage.setScene(scene);
9
detailsStage.show();
23
0 }
23
1 private void writeDataToFile(ObservableList<Student> studentList) throws IOException {
23 try (FileWriter writer = new FileWriter(FILE_PATH)) {
2
for (Student student : studentList) {
23
3 writer.write(student.getName() + "," + student.getId() + "," +
24
8
24
9 public static class Student {
25
3 public Student(String name, String id, String course) {
25 setName(name);
4
setId(id);
25
5 setCourse(course);
25 }
6
25
public StringProperty nameProperty() {
7
return name;
25
8 }
25
9
public StringProperty idProperty() {
26
return id;
0
}
26
1
26
public String getName() {
5
return name.get();
26
6 }
26
7
public void setName(String name) {
26
8 this.name.set(name);
26 }
9
27
2 public void setId(String id) {
27 this.id.set(id);
3
}
27
4
27
7 public void setCourse(String course) {
27 this.course.set(course);
8
}
27
9
public DoubleProperty gradeProperty() {
28
0 return grade;
28 }
1
28
2
28 public double getGrade() {
3
return grade.get();
28
}
4
28
5 public void setGrade(double grade) {
28 this.grade.set(grade);
6
}
28
7 }
28 }
8
28
9
29
0
29
1
29
2
29
3
29
4
29
5
29
6
29
7
29
8
29
9
30
0
30
1
30
2
30
3
30
4
30
5
30
6
30
7
30
8
30
9
31
0
31
1
31
2
31
3
31
4
31
5
31
6
31
7
31
8
31
9
32
0
32
1
32
2
32
3
32
4
32
5
32
6
32
7
32
8
32
9
33
0
33
1
33
2
33
3
33
4
33
5
33
6
33
7
33
8
33
9
34
0
34
1
34
2
34
3
34
4
34
5
34
6
34
7
34
8
34
9
35
0
35
1
35
2
35
3
35
4
35
5
35
6
35
7
35
8
35
9
36
0
36
1
36
2
36
3
36
4
36
5
36
6
36
7
36
8
36
9
37
0
37
1
37
2
37
3
37
4
37
5
37
6
37
7
37
8
37
9
38
0
38
1
38
2
38
3
38
4
38
5
38
6
38
7
38
8
38
9
39
0
39
1
39
2
39
3
39
4
39
5
39
6
39
7
39
8
Overview
This JavaFX application is designed for managing a small group of students, ideal for handling up to
100 students. It provides functionalities to add, update, delete, and save student data, including their
name, ID, course, and grade. The application features a user-friendly interface with a table view for
displaying student records and separate forms for adding and updating student details.
Features
Save Data: Offers the functionality to save the current state of student records to a CSV file.
Automatic Save Prompt: When closing the application, it prompts to save if unsaved changes
are detected.
How to Use
Run App.java to launch the application. The main window displays a table with student data.
2. Fill in the student's details in the new window and click "Add".
Updating a Student
1. Select a student from the table and click the "View Details" button.
Saving Data
Click the "Save Data" button to manually save the current data to students.csv.
On closing the application, if unsaved changes are detected, a prompt will ask whether to
save the changes.
Development Details
Technologies Used
File Structure
The application handles a small set of data (up to 100 students) and might not be suitable for
larger datasets. (Somehow there is a performance issues when exceeding 100 students data)
Basic error handling and data validation are implemented. Further enhancements may be
necessary for broader usage.
Future Enhancements
1. Student Table