-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoListAppGUI.java
More file actions
190 lines (163 loc) · 6.97 KB
/
ToDoListAppGUI.java
File metadata and controls
190 lines (163 loc) · 6.97 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ToDoListAppGUI {
private static final String FILE_NAME = "tasks.txt";
private static List<Task> tasks = new ArrayList<>();
private static DefaultTableModel tableModel;
public static void main(String[] args) {
SwingUtilities.invokeLater(ToDoListAppGUI::createAndShowGUI);
loadTasks();
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("To-Do List Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 400);
frame.setLayout(new BorderLayout());
// Table to display tasks
String[] columnNames = {"#", "Task", "Status", "Due Date"};
tableModel = new DefaultTableModel(columnNames, 0);
JTable taskTable = new JTable(tableModel);
taskTable.setFont(new Font("Arial", Font.PLAIN, 14));
taskTable.setRowHeight(24);
JScrollPane scrollPane = new JScrollPane(taskTable);
// Load existing tasks into the table
refreshTaskTable();
// Buttons
JButton addButton = new JButton("Add Task");
JButton markCompleteButton = new JButton("Mark Complete");
JButton deleteButton = new JButton("Delete Task");
JButton exitButton = new JButton("Exit");
addButton.setFont(new Font("Arial", Font.BOLD, 14));
markCompleteButton.setFont(new Font("Arial", Font.BOLD, 14));
deleteButton.setFont(new Font("Arial", Font.BOLD, 14));
exitButton.setFont(new Font("Arial", Font.BOLD, 14));
// Button Panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4, 10, 0));
buttonPanel.add(addButton);
buttonPanel.add(markCompleteButton);
buttonPanel.add(deleteButton);
buttonPanel.add(exitButton);
// Action Listeners
addButton.addActionListener(e -> addTask());
markCompleteButton.addActionListener(e -> markTaskComplete(taskTable.getSelectedRow()));
deleteButton.addActionListener(e -> deleteTask(taskTable.getSelectedRow()));
exitButton.addActionListener(e -> {
saveTasks();
JOptionPane.showMessageDialog(frame, "Goodbye!");
System.exit(0);
});
// Layout
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
// Show GUI
frame.setVisible(true);
}
private static void addTask() {
String taskName = JOptionPane.showInputDialog(null, "Enter task name:", "Add Task", JOptionPane.PLAIN_MESSAGE);
if (taskName == null || taskName.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "Task name cannot be empty.");
return;
}
String dueDateString = JOptionPane.showInputDialog(null, "Enter due date (yyyy-MM-dd):", "Add Task", JOptionPane.PLAIN_MESSAGE);
if (dueDateString == null || dueDateString.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "Due date cannot be empty.");
return;
}
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dueDate = dateFormat.parse(dueDateString);
tasks.add(new Task(taskName, false, dueDate));
refreshTaskTable();
JOptionPane.showMessageDialog(null, "Task added!");
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, "Invalid date format. Please use yyyy-MM-dd.");
}
}
private static void markTaskComplete(int index) {
if (index == -1) {
JOptionPane.showMessageDialog(null, "Please select a task to mark as complete.");
return;
}
tasks.get(index).setCompleted(true);
refreshTaskTable();
JOptionPane.showMessageDialog(null, "Task marked as complete!");
}
private static void deleteTask(int index) {
if (index == -1) {
JOptionPane.showMessageDialog(null, "Please select a task to delete.");
return;
}
tasks.remove(index);
refreshTaskTable();
JOptionPane.showMessageDialog(null, "Task deleted!");
}
private static void refreshTaskTable() {
tableModel.setRowCount(0); // Clear the table
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < tasks.size(); i++) {
Task task = tasks.get(i);
String dueDate = task.getDueDate() != null ? dateFormat.format(task.getDueDate()) : "No Date";
tableModel.addRow(new Object[]{i + 1, task.getName(), task.isCompleted() ? "Completed" : "Incomplete", dueDate});
}
}
private static void loadTasks() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";");
String name = parts[0];
boolean isCompleted = Boolean.parseBoolean(parts[1]);
Date dueDate = parts.length > 2 ? dateFormat.parse(parts[2]) : null;
tasks.add(new Task(name, isCompleted, dueDate));
}
} catch (IOException | ParseException e) {
System.out.println("No existing tasks found. Starting fresh.");
}
}
private static void saveTasks() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (Task task : tasks) {
String dueDate = task.getDueDate() != null ? dateFormat.format(task.getDueDate()) : "";
writer.write(task.getName() + ";" + task.isCompleted() + ";" + dueDate);
writer.newLine();
}
} catch (IOException e) {
System.out.println("Error saving tasks.");
}
}
}
class Task {
private String name;
private boolean isCompleted;
private Date dueDate;
public Task(String name, boolean isCompleted, Date dueDate) {
this.name = name;
this.isCompleted = isCompleted;
this.dueDate = dueDate;
}
public String getName() {
return name;
}
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean completed) {
isCompleted = completed;
}
public Date getDueDate() {
return dueDate;
}
public void setDueDate(Date dueDate) {
this.dueDate = dueDate;
}
}