-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoList.java
More file actions
136 lines (116 loc) · 4.58 KB
/
ToDoList.java
File metadata and controls
136 lines (116 loc) · 4.58 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
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ToDoList extends Application {
private final ObservableList<String> tasks = FXCollections.observableArrayList();
private final String FILE_PATH = "tasks.txt";
public void start(Stage stage) {
stage.setTitle("To-Do List App");
// ---- Title ----
Label title = new Label("My To-Do List");
title.setFont(Font.font("Poppins", 24));
title.setTextFill(Color.web("#2c3e50"));
// ---- Input ----
TextField taskInput = new TextField();
taskInput.setPromptText("Enter a new task...");
taskInput.setFont(Font.font("Poppins", 14));
taskInput.setPrefWidth(400);
taskInput.setStyle("-fx-background-radius: 10; -fx-padding: 8;");
// ---- Buttons ----
Button addButton = makeButton("Add", "#27ae60");
Button doneButton = makeButton("Done", "#2980b9");
Button deleteButton = makeButton("Delete", "#c0392b");
HBox buttonBox = new HBox(10, addButton, doneButton, deleteButton);
buttonBox.setAlignment(Pos.CENTER);
buttonBox.setPadding(new Insets(10));
// ---- Task List ----
ListView<String> listView = new ListView<>(tasks);
listView.setStyle(
"-fx-font-family: 'Poppins';\n"
+ "-fx-font-size: 14;\n"
+ "-fx-background-radius: 12;\n"
+ "-fx-control-inner-background: #f8f9fa;\n"
+ "-fx-border-color: #dcdde1;\n"
+ "-fx-border-radius: 12;"
);
// ---- Layout ----
VBox root = new VBox(15, title, listView, taskInput, buttonBox);
root.setPadding(new Insets(20));
root.setAlignment(Pos.CENTER);
root.setStyle("-fx-background-color: linear-gradient(to bottom right, #dff9fb, #c7ecee);");
// ---- Load saved tasks ----
loadTasksFromFile();
// ---- Button Actions ----
addButton.setOnAction(e -> {
String task = taskInput.getText().trim();
if (!task.isEmpty()) {
tasks.add(task);
taskInput.clear();
saveTasksToFile();
}
});
deleteButton.setOnAction(e -> {
String selected = listView.getSelectionModel().getSelectedItem();
if (selected != null) {
tasks.remove(selected);
saveTasksToFile();
}
});
doneButton.setOnAction(e -> {
String selected = listView.getSelectionModel().getSelectedItem();
if (selected != null && !selected.startsWith("[Done] ")) {
int index = tasks.indexOf(selected);
tasks.set(index, "[Done] " + selected);
saveTasksToFile();
}
});
// ---- Scene Setup ----
Scene scene = new Scene(root, 520, 480);
stage.setScene(scene);
stage.show();
}
// Helper to create styled buttons
private Button makeButton(String text, String color) {
Button btn = new Button(text);
btn.setFont(Font.font("Poppins", 14));
btn.setTextFill(Color.WHITE);
btn.setStyle("-fx-background-color: " + color + "; -fx-background-radius: 10;");
btn.setOnMouseEntered(e -> btn.setStyle("-fx-background-color: derive(" + color + ", 20%); -fx-background-radius: 10;"));
btn.setOnMouseExited(e -> btn.setStyle("-fx-background-color: " + color + "; -fx-background-radius: 10;"));
btn.setPrefWidth(100);
btn.setPrefHeight(35);
return btn;
}
private void loadTasksFromFile() {
try {
if (Files.exists(Paths.get(FILE_PATH))) {
tasks.addAll(Files.readAllLines(Paths.get(FILE_PATH)));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveTasksToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
for (String task : tasks) {
writer.write(task + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}