-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoListGUI.java
More file actions
113 lines (98 loc) · 3.43 KB
/
Copy pathToDoListGUI.java
File metadata and controls
113 lines (98 loc) · 3.43 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
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class ToDoListGUI {
private JFrame frame;
private JButton addButton;
private JPanel mainPanel;
private JPanel buttonPanel;
private JTextField userInput;
private JLabel label;
private DefaultListModel<String> taskMemory;
private JList<String> taskList;
private JButton removeButton;
private JButton clearAllTasksButton;
public ToDoListGUI () {
frame = new JFrame();
frame.setTitle("To Do List");
addButton = new JButton("Create new task");
removeButton = new JButton("Remove selected task");
clearAllTasksButton = new JButton("Remove all tasks");
label = new JLabel();
frame.setSize(500,400);
mainPanel = new JPanel(new BorderLayout());
userInput = new JTextField(20);
JPanel inputPanel = new JPanel();
inputPanel.add(userInput);
inputPanel.add(label);
taskMemory = new DefaultListModel<>();
taskList = new JList<>(taskMemory);
JScrollPane scrollPane = new JScrollPane(taskList);
scrollPane.setPreferredSize(new java.awt.Dimension(400, 200));
frame.add(mainPanel);
buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(clearAllTasksButton);
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addButton.addActionListener(new addTasks());
removeButton.addActionListener(new removeTasks());
clearAllTasksButton.addActionListener(new clearAllTasks());
frame.setVisible(true);
}
public void addTasks() {
String task = userInput.getText();
if (!task.isEmpty()) {
label.setText("Task: " + task + " has been added.");
taskMemory.addElement(task);
userInput.setText("");
}
}
public void removeTasks() {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
taskMemory.remove(selectedIndex);
label.setText("Task has been removed.");
}
}
public void clearAllTasks() {
if (!taskMemory.isEmpty()) {
taskMemory.clear();
label.setText("All tasks have been removed.");
} else {
label.setText("Task list is empty. There are no tasks to remove.");
}
}
private class addTasks implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
addTasks();
}
}
private class removeTasks implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
removeTasks();
}
}
private class clearAllTasks implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
clearAllTasks();
}
}
public static void main(String[] args) {
new ToDoListGUI();
}
}