-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoApp.java
More file actions
288 lines (249 loc) · 12.1 KB
/
Copy pathToDoApp.java
File metadata and controls
288 lines (249 loc) · 12.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* ToDoApp is a Java Swing-based desktop application that provides
* a clean and user-friendly interface to manage a To-Do list.
* Users can enter tasks into a text field, add them to the list,
* select any task, and delete it.
*
* Features:
* - Clean, modern, responsive desktop GUI layout.
* - System Look and Feel support for native window elements.
* - Add/Delete task actions with validation dialogs.
* - ActionListeners bound to both buttons and keyboard events (Enter key).
* - Dynamic list rendering with alternating row colors (zebra striping).
* - Real-time task count status indicator.
*
* Suitable as a Java developer internship assignment.
*
* Author: Mummana Devi Prasad
*/
public class ToDoApp {
// Main window container
private JFrame frame;
// Input field for writing new task items
private JTextField taskField;
// List component showing all task items
private JList<String> taskList;
// Underlying data model that allows adding/removing list elements dynamically
private DefaultListModel<String> listModel;
// Interactive buttons for user operations
private JButton addButton;
private JButton deleteButton;
// Label display showing total count of current tasks
private JLabel statusLabel;
/**
* Constructor for ToDoApp. Initializes components and constructs the GUI.
*/
public ToDoApp() {
createAndShowGUI();
}
/**
* Builds, styles, and displays the main user interface.
*/
private void createAndShowGUI() {
// Try to apply system's native look and feel for a cleaner, modern interface
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// Fallback to cross-platform Look & Feel if system-specific L&F fails
System.err.println("Native Look & Feel could not be set. Using default. Error: " + e.getMessage());
}
// Initialize the main window JFrame
frame = new JFrame("Task Planner - My To-Do List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450, 520);
frame.setMinimumSize(new Dimension(350, 400)); // Enforce minimum window size for responsiveness
frame.setLocationRelativeTo(null); // Centers window on user's screen
// Create main layout panel with spacious padding
JPanel mainPanel = new JPanel(new BorderLayout(15, 15));
mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
mainPanel.setBackground(new Color(245, 247, 250)); // Modern light-blue-gray background
frame.setContentPane(mainPanel);
// --- 1. HEADER PANEL ---
// Contains application title and quick description
JPanel headerPanel = new JPanel();
headerPanel.setLayout(new BoxLayout(headerPanel, BoxLayout.Y_AXIS));
headerPanel.setBackground(new Color(245, 247, 250));
JLabel titleLabel = new JLabel("My Tasks");
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 26));
titleLabel.setForeground(new Color(31, 41, 55)); // Muted charcoal/dark text
titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel subtitleLabel = new JLabel("Manage your daily activities and goals");
subtitleLabel.setFont(new Font("Segoe UI", Font.PLAIN, 13));
subtitleLabel.setForeground(new Color(107, 114, 128)); // Soft neutral gray
subtitleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
headerPanel.add(titleLabel);
headerPanel.add(Box.createRigidArea(new Dimension(0, 4))); // Small spacer
headerPanel.add(subtitleLabel);
mainPanel.add(headerPanel, BorderLayout.NORTH);
// --- 2. LIST PANEL (CENTER) ---
// Dynamically displays tasks, utilizing a DefaultListModel to handle data changes.
listModel = new DefaultListModel<>();
// Add default tasks as starting suggestions/examples
listModel.addElement("Finish the Java internship report");
listModel.addElement("Read about Java Swing & Layout Managers");
listModel.addElement("Review the MVC design pattern basics");
taskList = new JList<>(listModel);
taskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
taskList.setFont(new Font("Segoe UI", Font.PLAIN, 14));
taskList.setSelectionBackground(new Color(224, 231, 255)); // Indigo highlighted selection background
taskList.setSelectionForeground(new Color(67, 56, 202)); // Deep indigo text for selected row
// Custom Cell Renderer: Adds modern padding, separator lines, and alternating row colors (zebra striping)
taskList.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
// Get standard label configuration
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
// Add inner text padding and a light gray bottom border to separate items
label.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(243, 244, 246)),
BorderFactory.createEmptyBorder(12, 15, 12, 15)
));
// Zebra striping color logic when the item is NOT selected
if (!isSelected) {
label.setBackground(index % 2 == 0 ? Color.WHITE : new Color(250, 250, 250));
label.setForeground(new Color(55, 65, 81));
}
return label;
}
});
// JScrollPane is required to support lists that exceed the initial window height
JScrollPane scrollPane = new JScrollPane(taskList);
scrollPane.setBorder(BorderFactory.createLineBorder(new Color(229, 231, 235), 1));
scrollPane.getViewport().setBackground(Color.WHITE);
mainPanel.add(scrollPane, BorderLayout.CENTER);
// --- 3. INPUT AND CONTROL PANEL (SOUTH) ---
// Holds task entry field, operational action buttons, and status label.
JPanel southPanel = new JPanel();
southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
southPanel.setBackground(new Color(245, 247, 250));
// Sub-panel for text input
JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
inputPanel.setBackground(new Color(245, 247, 250));
taskField = new JTextField();
taskField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
taskField.setToolTipText("Enter task description here");
// Modern input styling with comfortable margin/padding
taskField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(209, 213, 219), 1),
BorderFactory.createEmptyBorder(10, 12, 10, 12)
));
inputPanel.add(taskField, BorderLayout.CENTER);
southPanel.add(inputPanel);
southPanel.add(Box.createRigidArea(new Dimension(0, 12))); // Vertical gap
// Action Buttons Row: Grid layout ensures buttons have uniform sizes
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 15, 0));
buttonPanel.setBackground(new Color(245, 247, 250));
// Create and style "Add Task" button
addButton = new JButton("Add Task");
addButton.setFont(new Font("Segoe UI", Font.BOLD, 14));
addButton.setBackground(new Color(79, 70, 229)); // Indigo blue theme
addButton.setForeground(Color.WHITE);
addButton.setFocusPainted(false);
addButton.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0));
addButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Create and style "Delete Task" button
deleteButton = new JButton("Delete Task");
deleteButton.setFont(new Font("Segoe UI", Font.BOLD, 14));
deleteButton.setBackground(new Color(239, 68, 68)); // Soft crimson red theme
deleteButton.setForeground(Color.WHITE);
deleteButton.setFocusPainted(false);
deleteButton.setBorder(BorderFactory.createEmptyBorder(12, 0, 12, 0));
deleteButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
southPanel.add(buttonPanel);
southPanel.add(Box.createRigidArea(new Dimension(0, 12))); // Vertical gap
// Status bar panel showing task counts
JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
statusPanel.setBackground(new Color(245, 247, 250));
statusLabel = new JLabel("Total Tasks: " + listModel.getSize());
statusLabel.setFont(new Font("Segoe UI", Font.ITALIC, 12));
statusLabel.setForeground(new Color(156, 163, 175)); // Muted gray text
statusPanel.add(statusLabel);
southPanel.add(statusPanel);
mainPanel.add(southPanel, BorderLayout.SOUTH);
// --- 4. ACTION LISTENERS (EVENT HANDLING) ---
// Shared action handler to add a new task item
ActionListener addTaskListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addTask();
}
};
// Add task when clicking the button OR pressing "Enter" inside the text field
addButton.addActionListener(addTaskListener);
taskField.addActionListener(addTaskListener);
// Delete task action listener
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteTask();
}
});
// Finally, make the window frame visible to user
frame.setVisible(true);
}
/**
* Retrieves the text from the input field, validates it, and adds it to the list model.
*/
private void addTask() {
String taskText = taskField.getText().trim();
if (taskText.isEmpty()) {
// Trigger warning dialog if field is blank
JOptionPane.showMessageDialog(frame,
"Task content cannot be empty! Please type a description.",
"Input Warning",
JOptionPane.WARNING_MESSAGE);
} else {
// Append task to the dynamic list model
listModel.addElement(taskText);
// Reset input field and bring cursor focus back
taskField.setText("");
taskField.requestFocusInWindow();
// Re-render task count status
updateStatusLabel();
}
}
/**
* Determines the currently selected task and deletes it.
*/
private void deleteTask() {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex == -1) {
// Trigger warning dialog if deletion was clicked without picking a target
JOptionPane.showMessageDialog(frame,
"Please select a task from the list below to delete it.",
"Selection Warning",
JOptionPane.WARNING_MESSAGE);
} else {
// Remove selection index from the list model
listModel.remove(selectedIndex);
// Re-render task count status
updateStatusLabel();
}
}
/**
* Calculates the list model size and updates the status label text.
*/
private void updateStatusLabel() {
statusLabel.setText("Total Tasks: " + listModel.getSize());
}
/**
* Entry point of the program.
*/
public static void main(String[] args) {
// Safely schedule the GUI construction task on the Event Dispatch Thread (EDT)
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ToDoApp();
}
});
}
}