App.
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
24 public class App extends Application {
25
26 private ObservableList<Student> studentList = FXCollections.observableArrayList();
27 private TableView<Student> tableView = new TableView<>();
28 private ComboBox<String> courseComboBox = new ComboBox<>();
29
30 // File path for the CSV file
31 private static final String FILE_PATH = "students.csv";
32
33 public static void main(String[] args) {
34 launch(args);
35 }
36
37 @Override
38 public void start(Stage primaryStage) throws IOException {
39 studentList = readDataFromFile();
40 primaryStage.setTitle("Student Management System");
41
42 // Create a table to display student records
43 TableColumn<Student, String> nameColumn = new TableColumn<>("Name");
44 nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
45
46 TableColumn<Student, String> idColumn = new TableColumn<>("ID");
47 idColumn.setCellValueFactory(cellData -> cellData.getValue().idProperty());
48
49 TableColumn<Student, String> courseColumn = new TableColumn<>("Course");
50 courseColumn.setCellValueFactory(cellData -> cellData.getValue().courseProperty());
51
52 tableView.getColumns().add(nameColumn);
53 tableView.getColumns().add(idColumn);
54 tableView.getColumns().add(courseColumn);
55 tableView.setItems(studentList);
56
57 // Column for Grades
58 TableColumn<Student, Number> gradeColumn = new TableColumn<>("Grade");
59 gradeColumn.setCellValueFactory(cellData -> cellData.getValue().gradeProperty());
60 tableView.getColumns().add(gradeColumn);
61
62 // Adding a button to each row in the table to view details
63 TableColumn<Student, Void> detailsColumn = new TableColumn<>("Details");
64 detailsColumn.setCellFactory(param -> new TableCell<Student, Void>() {
65 private final Button detailsButton = new Button("View Details");
66
67 {
68 detailsButton.setOnAction(event -> {
69 Student student = getTableView().getItems().get(getIndex());
70 showStudentDetails(student);
71 });
72 }
73
74 @Override
75 protected void updateItem(Void item, boolean empty) {
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
86 // Add Student Button
87 Button addStudentButton = new Button("Add Student");
88 addStudentButton.setOnAction(e -> openAddStudentForm());
89
90 // Save Data Button
91 Button saveButton = new Button("Save Data");
92 saveButton.setOnAction(e -> {
93 // Create a popup for the progress indicator
94 Stage progressStage = new Stage();
95 progressStage.initModality(Modality.APPLICATION_MODAL);
96 progressStage.setTitle("Saving Data");
97
98 ProgressIndicator progressIndicator = new ProgressIndicator();
99 VBox progressBox = new VBox(10, new Label("Saving..."), progressIndicator);
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 new Thread(() -> {
5 try {
10 writeDataToFile(studentList);
6
Platform.runLater(() -> {
10
7 progressStage.close(); // Close the progress indicator popup
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 alert.setContentText("Data saved to students.csv");
1
alert.showAndWait();
11
2 });
11 } catch (IOException ex) {
3
Platform.runLater(() -> {
11
progressStage.close(); // Close the progress indicator popup
4
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.setContentText("An error occurred while saving data: " +
8 ex.getMessage());
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 courseComboBox.getItems().addAll("Math", "Science", "History", "English");
8
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 // Create a layout that combines inputGrid and buttonLayout
6 VBox bottomLayout = new VBox();
13 bottomLayout.getChildren().addAll(inputGrid, buttonLayout);
7
borderPane.setBottom(bottomLayout);
13
8
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 confirmDialog.setContentText("Would you like to save your last update?");
2
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 ObservableList<Student> fileData = readDataFromFile();
5 // Using equals method for comparison
15 return !studentList.equals(fileData);
6
} catch (IOException e) {
15
7 e.printStackTrace();
15 return true; // Assume data is different if there's an error reading the file
8 }
15 }
9
16
0
16 private void openAddStudentForm() {
1
Stage addStudentStage = new Stage();
16
addStudentStage.setTitle("Add New Student");
2
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
17 VBox formLayout = new VBox(10);
8
formLayout.setPadding(new Insets(20));
17
9 formLayout.getChildren().addAll(
18 new Label("Name:"), nameField,
0
new Label("ID:"), idField,
18
new Label("Course:"), courseComboBox,
1
addButton
18
2 );
18
3
addStudentStage.setScene(new Scene(formLayout, 300, 300));
18
addStudentStage.show();
4
}
18
5
18 private void showStudentDetails(Student student) {
6
Stage detailsStage = new Stage();
18
7
18 detailsStage.setTitle("Student Details");
8
18
TextField nameField = new TextField(student.getName());
9
TextField idField = new TextField(student.getId());
19
0
19 // ComboBox for course selection
1
ComboBox<String> courseComboBox = new ComboBox<>();
19
2 courseComboBox.getItems().addAll("Math", "Science", "History", "English"); // Add your
courses here
19
3 courseComboBox.setValue(student.getCourse()); // Set to current course
19
4 TextField gradeField = new TextField(String.valueOf(student.getGrade()));
19
5
Button updateButton = new Button("Update");
19
6 updateButton.setOnAction(e -> {
19 // Update the student's information
7 student.setName(nameField.getText());
19 student.setId(idField.getText());
8
student.setCourse(courseComboBox.getValue());
19
9 try {
20 double grade = Double.parseDouble(gradeField.getText());
0
student.setGrade(grade);
20
} catch (NumberFormatException ex) {
1
// Handle invalid grade input
20
2 }
20 tableView.refresh(); // Refresh the table to show updated data
3
detailsStage.close(); // Close the details window
20
});
4
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 ButtonType buttonTypeCancel = new ButtonType("Cancel",
2 ButtonBar.ButtonData.CANCEL_CLOSE);
21 confirmDialog.getButtonTypes().setAll(buttonTypeYes, buttonTypeCancel);
3
21 Optional<ButtonType> result = confirmDialog.showAndWait();
4
if (result.isPresent() && result.get() == buttonTypeYes) {
21
5 studentList.remove(student);
21 tableView.refresh();
6 detailsStage.close();
21 }
7
});
21
8
21 // Layout for buttons
9
HBox buttonLayout = new HBox(10);
22
buttonLayout.getChildren().addAll(updateButton, deleteButton);
0
22
1 // Layout for the form
22 VBox layout = new VBox(10);
2
layout.setPadding(new Insets(20));
22
layout.getChildren().addAll(
3
new Label("Name:"), nameField,
22
4 new Label("ID:"), idField,
22 new Label("Course:"), courseComboBox,
5
22 new Label("Grade:"), gradeField,
6
buttonLayout
22
);
7
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() + "," +
23 student.getCourse() + "," + student.getGrade() + "\n");
4 }
23 }
5
}
23
6
23 private ObservableList<Student> readDataFromFile() throws IOException {
7
ObservableList<Student> tempList = FXCollections.observableArrayList();
23
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
8
String line;
23
9 while ((line = reader.readLine()) != null) {
24 String[] data = line.split(",");
0
Student student = new Student(data[0], data[1], data[2]);
24
student.setGrade(Double.parseDouble(data[3]));
1
tempList.add(student);
24
2 }
24 } catch (FileNotFoundException e) {
3
// Handle the case where the file does not exist
24
4
24 System.out.println("No existing file found. A new file will be created.");
5
}
24
return tempList;
6
}
24
7
24
8
24
9 public static class Student {
25 private final StringProperty name = new SimpleStringProperty();
0 private final StringProperty id = new SimpleStringProperty();
25 private final StringProperty course = new SimpleStringProperty();
1
private final DoubleProperty grade = new SimpleDoubleProperty(0.0);
25
2
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 StringProperty courseProperty() {
2
return course;
26
3
26 }
4
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 public String getId() {
0
return id.get();
27
1 }
27
2 public void setId(String id) {
27 this.id.set(id);
3
}
27
4
27 public String getCourse() {
5
return course.get();
27
}
6
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
Complete code can be reviewed at:
https://github.com/ChrisnaBadar/studentManagementSystem
Documentation for Student Management System (JavaFX Application)
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
Add Student: Allows users to add new student records.
Update Student: Enables users to update existing student records.
Delete Student: Facilitates the removal of student records.
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
Starting the Application
Run App.java to launch the application. The main window displays a table with student data.
Adding a New Student
1. Click the "Add Student" button.
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.
2. Update the details in the pop-up window and click "Update".
Deleting a Student
1. Select a student and click the "View Details" button.
2. In the details window, click "Delete" and confirm the deletion.
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
JavaFX for GUI
Java I/O for file handling
File Structure
App.java: Main application file containing the GUI and logic.
Student.java: Class representing the student data model.
students.csv: File where student data is saved.
Known Issues and Limitations
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
Improved error handling and user feedback mechanisms.
Enhanced data validation for form inputs.
Additional features like search and sort functionality.
Application Screenshot:
1. Student Table
2. Add Student Data
3. Student data update (course and grade) or delete data.