-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoApp_Q25.java
More file actions
236 lines (192 loc) · 7.94 KB
/
TodoApp_Q25.java
File metadata and controls
236 lines (192 loc) · 7.94 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
/**
* TodoApp_Q25.java
*
* This program implements a simple To-Do application using JavaFX.
* It allows users to add, edit, delete, and mark tasks as completed.
*/
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Callback;
import java.time.LocalDate;
public class TodoApp_Q25 extends Application {
// Model: Task class to represent a to-do item
private static class Task {
private final String description;
private final LocalDate dueDate;
private boolean completed;
public Task(String description, LocalDate dueDate) {
this.description = description;
this.dueDate = dueDate;
this.completed = false;
}
public String getDescription() {
return description;
}
public LocalDate getDueDate() {
return dueDate;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
@Override
public String toString() {
return description + " (Due: " + dueDate + ")";
}
}
// Data model: list of tasks
private final ObservableList<Task> taskList = FXCollections.observableArrayList();
// UI components
private ListView<Task> taskListView;
private TextField taskDescriptionField;
private DatePicker dueDatePicker;
@Override
public void start(Stage primaryStage) {
// Set up the primary stage
primaryStage.setTitle("To-Do Application");
// Create the main layout
BorderPane root = new BorderPane();
root.setPadding(new Insets(10));
// Create the task input form
GridPane inputForm = createInputForm_Q25();
root.setTop(inputForm);
// Create the task list view
taskListView = new ListView<>(taskList);
taskListView.setCellFactory(createCellFactory_Q25());
root.setCenter(taskListView);
// Create the button panel
HBox buttonPanel = createButtonPanel_Q25();
root.setBottom(buttonPanel);
// Create the scene and show the stage
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
// Add some sample tasks
addSampleTasks_Q25();
}
private GridPane createInputForm_Q25() {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 0, 10, 0));
// Task description input
Label descriptionLabel = new Label("Task Description:");
taskDescriptionField = new TextField();
taskDescriptionField.setPromptText("Enter task description");
// Due date input
Label dueDateLabel = new Label("Due Date:");
dueDatePicker = new DatePicker();
dueDatePicker.setValue(LocalDate.now());
// Add button
Button addButton = new Button("Add Task");
addButton.setOnAction(e -> addTask_Q25());
// Add components to the grid
grid.add(descriptionLabel, 0, 0);
grid.add(taskDescriptionField, 1, 0);
grid.add(dueDateLabel, 0, 1);
grid.add(dueDatePicker, 1, 1);
grid.add(addButton, 1, 2);
// Set column constraints
ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(30);
ColumnConstraints col2 = new ColumnConstraints();
col2.setPercentWidth(70);
grid.getColumnConstraints().addAll(col1, col2);
return grid;
}
private HBox createButtonPanel_Q25() {
HBox buttonPanel = new HBox(10);
buttonPanel.setPadding(new Insets(10, 0, 0, 0));
Button deleteButton = new Button("Delete Task");
deleteButton.setOnAction(e -> deleteTask_Q25());
Button markCompletedButton = new Button("Mark as Completed");
markCompletedButton.setOnAction(e -> markTaskAsCompleted_Q25());
Button clearAllButton = new Button("Clear All");
clearAllButton.setOnAction(e -> clearAllTasks_Q25());
buttonPanel.getChildren().addAll(deleteButton, markCompletedButton, clearAllButton);
return buttonPanel;
}
private Callback<ListView<Task>, ListCell<Task>> createCellFactory_Q25() {
return listView -> new ListCell<Task>() {
@Override
protected void updateItem(Task task, boolean empty) {
super.updateItem(task, empty);
if (empty || task == null) {
setText(null);
setStyle("");
} else {
setText(task.getDescription() + " (Due: " + task.getDueDate() + ")");
// Apply different styles based on task status
if (task.isCompleted()) {
setStyle("-fx-text-fill: gray; -fx-strikethrough: true;");
} else if (task.getDueDate().isBefore(LocalDate.now())) {
setStyle("-fx-text-fill: red;"); // Overdue tasks
} else if (task.getDueDate().equals(LocalDate.now())) {
setStyle("-fx-text-fill: orange;"); // Due today
} else {
setStyle("");
}
}
}
};
}
private void addTask_Q25() {
String description = taskDescriptionField.getText().trim();
LocalDate dueDate = dueDatePicker.getValue();
if (!description.isEmpty() && dueDate != null) {
Task newTask = new Task(description, dueDate);
taskList.add(newTask);
// Clear the input fields
taskDescriptionField.clear();
dueDatePicker.setValue(LocalDate.now());
taskDescriptionField.requestFocus();
} else {
showAlert_Q25("Invalid Input", "Please enter a task description and select a due date.");
}
}
private void deleteTask_Q25() {
int selectedIndex = taskListView.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
taskList.remove(selectedIndex);
} else {
showAlert_Q25("No Selection", "Please select a task to delete.");
}
}
private void markTaskAsCompleted_Q25() {
int selectedIndex = taskListView.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
Task selectedTask = taskList.get(selectedIndex);
selectedTask.setCompleted(!selectedTask.isCompleted()); // Toggle completed status
// Refresh the list view to update the display
taskListView.refresh();
} else {
showAlert_Q25("No Selection", "Please select a task to mark as completed.");
}
}
private void clearAllTasks_Q25() {
taskList.clear();
}
private void addSampleTasks_Q25() {
taskList.add(new Task("Complete Java assignment", LocalDate.now().plusDays(1)));
taskList.add(new Task("Prepare presentation", LocalDate.now().plusDays(3)));
taskList.add(new Task("Study for exam", LocalDate.now().plusDays(7)));
}
private void showAlert_Q25(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}