To Do List
RepositoryThis project is a simple To Do List application written in Java for Desktop. It includes the whole life cycle of an application.
Main View
The main view of the application is where the user can see all their lists and create new ones. It uses a VBox to arrange the buttons vertically and a ScrollPane to allow the user to scroll through their lists.
1/* Main View */
2@Override
3public void start(Stage mainStage) {
4 allLists = saveSystem.loadData();
5 // Title label
6 Label label = new Label("To Do List App");
7 label.getStyleClass().add("title-label");
8
9 // Create List Button
10 Button createList = new Button("Create a New List");
11 createList.setOnAction(e -> showCreateListPopup(mainStage));
12 createList.getStyleClass().add("create-list-button");
13
14 // ScrollPane for the listGroup
15 ScrollPane scrollPane = new ScrollPane(listGroup);
16 scrollPane.setFitToWidth(true);
17 scrollPane.setPannable(true);
18 scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
19 scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
20
21 // Delete list button
22 Button deleteList = new Button("Delete a List");
23 deleteList.setOnAction(e -> showDeleteListPopup(mainStage));
24 deleteList.getStyleClass().add("delete-list-button");
25
26 // Layout for main stage
27 VBox mainVBox = new VBox(10);
28 mainVBox.getChildren().addAll(label, createList, deleteList, scrollPane);
29
30 HBox mainHBox = new HBox(20);
31 mainHBox.setPadding(new Insets(0, 0, 0, 10));
32 mainHBox.getChildren().add(mainVBox);
33
34 // Initial Scene setup
35 updateListGroup(mainStage);
36 Scene scene = new Scene(mainHBox, 350, 600);
37 scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
38 mainStage.setTitle("To Do List App");
39 mainStage.setScene(scene);
40 mainStage.show();
41
42 mainScene = scene;
43}
Save System
The save system uses the org.json library to save and load the data from a JSON file. This allows the user to keep their data even after closing the application.
1public void saveData(List<ToDoList> list) {
2 // Create a JSONArray directly from the list
3 JSONArray jsonArray = new JSONArray();
4
5 // Convert each ToDoList object to JSON and add it to the JSONArray
6 for (ToDoList toDoList : list) {
7 jsonArray.put(toDoList.toJSON());
8 }
9
10 try (FileWriter file = new FileWriter(filePath)) {
11 // This will overwrite the file
12 file.write(jsonArray.toString(2)); // Pretty print with an indent factor of 2
13 System.out.println("Data saved to " + filePath);
14 } catch (IOException e) {
15 System.err.println("Error saving data: " + e.getMessage());
16 }
17}
18
19// Load data from the file
20public List<ToDoList> loadData() {
21 List<ToDoList> data = new ArrayList<>();
22
23 try (FileReader fileReader = new FileReader(filePath)) {
24 StringBuilder sb = new StringBuilder();
25 int content;
26 while ((content = fileReader.read()) != -1) {
27 sb.append((char) content); // Build the file content as a string
28 }
29 // Convert the string content to a JSONArray
30 JSONArray jsonArray = new JSONArray(sb.toString());
31 // Convert each JSON object to a ToDoList object and add it to the list
32 for (int i = 0; i < jsonArray.length(); i++) {
33 data.add(ToDoList.fromJSON(jsonArray.getJSONObject(i)));
34 }
35 }
36}
ToDoList Logic
The ToDoList class contains all the logic for managing tasks, such as adding, deleting, and sorting tasks. It also updates the completion percentage of the list.
1public void updateCompletionPercentage() {
2 completedTasks.clear();
3 for (Task task : allTasks) {
4 if (task.getTaskStatus() == TaskStatus.COMPLETED) completedTasks.add(task);
5 }
6 DecimalFormat df = new DecimalFormat("#.#");
7 completionPercentage = Double.parseDouble(df.format((double) completedTasks.size() / allTasks.size() * 100));
8
9 if (Double.isNaN(completionPercentage)) completionPercentage = 0;
10}
11
12public void sortList() {
13 SortTasks sortTasks = new SortTasks(this.sortingOptions);
14 if (this.sortingOptions == null) this.sortingOptions = SortingOptions.UNCATEGORIZED;
15 displayTaskList = sortTasks.sortTasks(allTasks);
16 System.out.println("Sorted " + allTasks + " to " + displayTaskList + " with " + sortingOptions.toFormattedString());
17}
18
19public void addTask(Task task) {
20 allTasks.add(task);
21 sortList();
22}
23
24public void deleteTask(Task task) {
25 allTasks.remove(task);
26 sortList();
27}
Try it out yourself!
DownloadIf you want to try this app out for yourself, click on the download button and install from GitHub.
Video Demonstration
This video requires optional cookies (YouTube).