-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoApp.java
More file actions
174 lines (140 loc) · 6.27 KB
/
Copy pathToDoApp.java
File metadata and controls
174 lines (140 loc) · 6.27 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
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
public class ToDoApp {
private JFrame frame;
private JPanel taskListPanel;
private JTextField taskInputField;
private JButton addTaskButton;
private ArrayList<Task> tasks;
public ToDoApp() {
tasks = new ArrayList<>();
frame = new JFrame("To-Do List");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// Title label
JLabel titleLabel = new JLabel("My To-Do List");
titleLabel.setFont(new Font("SansSerif", Font.BOLD, 20));
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(titleLabel, BorderLayout.NORTH);
// Task list panel with scrollable area
taskListPanel = new JPanel();
taskListPanel.setLayout(new BoxLayout(taskListPanel, BoxLayout.Y_AXIS)); // Stack tasks vertically
JScrollPane scrollPane = new JScrollPane(taskListPanel);
frame.add(scrollPane, BorderLayout.CENTER);
// Input and add button panel
JPanel inputPanel = new JPanel(new BorderLayout());
taskInputField = new JTextField();
taskInputField.setFont(new Font("SansSerif", Font.PLAIN, 14));
taskInputField.setBackground(Color.decode("#f7f7f7")); // Pastel input field
taskInputField.setForeground(Color.decode("#333333"));
inputPanel.add(taskInputField, BorderLayout.CENTER);
addTaskButton = createButton("Add Task", "#a5d6a7"); // Pastel green
inputPanel.add(addTaskButton, BorderLayout.EAST);
frame.add(inputPanel, BorderLayout.SOUTH);
// Add action listener for the "Add Task" button
addTaskButton.addActionListener(e -> {
((JButton) e.getSource()).setText("Add Task");
addTask();
});
// Add hover effect to the button
addTaskButton.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
addTaskButton.setBackground(Color.decode("#c8e6c9")); // Lighter pastel green on hover
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
addTaskButton.setBackground(Color.decode("#a5d6a7")); // Original pastel green
}
});
loadTasks();
frame.setVisible(true);
}
private JButton createButton(String text, String bgColor) {
JButton button = new JButton(text);
button.setFont(new Font("SansSerif", Font.PLAIN, 14));
button.setBackground(Color.decode(bgColor));
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
return button;
}
private void addTask() {
String taskTitle = taskInputField.getText().trim();
if (!taskTitle.isEmpty()) {
Task newTask = new Task(taskTitle);
tasks.add(newTask);
addTaskToUI(newTask);
taskInputField.setText("");
saveTasks();
}
}
private void addTaskToUI(Task task) {
JPanel taskPanel = new JPanel(new BorderLayout()); // Use BorderLayout for easy positioning
taskPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
taskPanel.setPreferredSize(new Dimension(600, 50)); // Make the task occupy full width but minimal height
taskPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 60)); // Ensure it takes up full width horizontally
taskPanel.setBackground(task.isCompleted() ? new Color(204, 255, 204) : new Color(255, 204, 204)); // Pastel colors
// Task Label (leftmost part)
JLabel taskLabel = new JLabel(task.getTitle());
taskLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
taskPanel.add(taskLabel, BorderLayout.CENTER); // Add the task label to the center
// Panel for buttons (to align them right)
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // Right alignment for buttons
buttonPanel.setOpaque(false); // Make button panel background transparent, so it blends with taskPanel
taskPanel.add(buttonPanel, BorderLayout.EAST);
// Done button - change color to green (#25d366)
JButton doneButton = createButton("Done", "#25d366"); // Green color
doneButton.addActionListener(e -> {
((JButton) e.getSource()).setText("Done");
markTaskAsDone(task, taskPanel);
});
buttonPanel.add(doneButton);
// Delete button - change color to blue (#4bbef3)
JButton deleteButton = createButton("Delete", "#4bbef3"); // Blue color
deleteButton.addActionListener(e -> {
((JButton) e.getSource()).setText("Delete");
deleteTask(task, taskPanel);
});
buttonPanel.add(deleteButton);
// Add task panel to the main list
taskListPanel.add(taskPanel);
taskListPanel.revalidate();
taskListPanel.repaint();
}
private void markTaskAsDone(Task task, JPanel taskPanel) {
task.toggleCompletion();
taskPanel.setBackground(task.isCompleted() ? new Color(204, 255, 204) : new Color(255, 204, 204)); // Update color
saveTasks();
}
private void deleteTask(Task task, JPanel taskPanel) {
tasks.remove(task);
taskListPanel.remove(taskPanel);
taskListPanel.revalidate();
taskListPanel.repaint();
saveTasks();
}
@SuppressWarnings("unchecked")
private void loadTasks() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("tasks.dat"))) {
tasks = (ArrayList<Task>) ois.readObject();
for (Task task : tasks) {
addTaskToUI(task);
}
} catch (IOException | ClassNotFoundException e) {
tasks = new ArrayList<>();
}
}
private void saveTasks() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tasks.dat"))) {
oos.writeObject(tasks);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ToDoApp::new);
}
}