-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoListApp.java
More file actions
57 lines (46 loc) · 1.88 KB
/
ToDoListApp.java
File metadata and controls
57 lines (46 loc) · 1.88 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ToDoListApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(ToDoListApp::createAndShowGUI);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("To-Do List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());
DefaultListModel<String> listModel = new DefaultListModel<>();
JList<String> taskList = new JList<>(listModel);
JScrollPane scrollPane = new JScrollPane(taskList);
JTextField taskField = new JTextField();
JButton addButton = new JButton("Add Task");
JButton removeButton = new JButton("Remove Selected");
JButton clearButton = new JButton("Clear All");
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(taskField, BorderLayout.CENTER);
inputPanel.add(addButton, BorderLayout.EAST);
JPanel buttonPanel = new JPanel();
buttonPanel.add(removeButton);
buttonPanel.add(clearButton);
frame.add(inputPanel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.SOUTH);
addButton.addActionListener(e -> {
String task = taskField.getText().trim();
if (!task.isEmpty()) {
listModel.addElement(task);
taskField.setText("");
}
});
removeButton.addActionListener(e -> {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
listModel.remove(selectedIndex);
}
});
clearButton.addActionListener(e -> listModel.clear());
frame.setVisible(true);
}
}