-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTo-Do List.java
More file actions
63 lines (51 loc) · 1.98 KB
/
Copy pathTo-Do List.java
File metadata and controls
63 lines (51 loc) · 1.98 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ToDoListApp extends JFrame {
JTextField taskField;
JButton addButton, deleteButton;
JPanel taskPanel;
public ToDoListApp() {
setTitle("To-Do List ");
setSize(600, 400);
setLayout(new BorderLayout());
taskField = new JTextField(20);
addButton = new JButton("Add Task");
deleteButton = new JButton("Delete Completed Tasks");
JPanel inputPanel = new JPanel();
inputPanel.add(taskField);
inputPanel.add(addButton);
inputPanel.add(deleteButton);
add(inputPanel, BorderLayout.NORTH);
taskPanel = new JPanel();
taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(taskPanel);
add(scrollPane, BorderLayout.CENTER);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String taskText = taskField.getText();
JCheckBox taskCheckBox = new JCheckBox(taskText);
taskPanel.add(taskCheckBox);
taskPanel.revalidate();
taskField.setText("");
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Component[] components = taskPanel.getComponents();
for (int i = components.length - 1; i >= 0; i--) {
JCheckBox box = (JCheckBox) components[i];
if (box.isSelected()) {
taskPanel.remove(box);
}
}
taskPanel.revalidate();
taskPanel.repaint();
}
});
}
public static void main(String[] args) {
new ToDoListApp().setVisible(true); // Create an instance of the ToDo List app and make it visible
}
}