-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoApp.java
More file actions
67 lines (56 loc) · 2.3 KB
/
Copy pathToDoApp.java
File metadata and controls
67 lines (56 loc) · 2.3 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ToDoApp {
public static void main(String[] args) {
// Frame
JFrame frame = new JFrame("To-Do List");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// Components
DefaultListModel<String> taskListModel = new DefaultListModel<>();
JList<String> taskList = new JList<>(taskListModel);
JScrollPane scrollPane = new JScrollPane(taskList);
JTextField taskField = new JTextField();
JButton addButton = new JButton("Add Task");
JButton deleteButton = new JButton("Delete Task");
// Panel for input & buttons
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BorderLayout());
inputPanel.add(taskField, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
// Add components to frame
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(inputPanel, BorderLayout.NORTH);
frame.add(buttonPanel, BorderLayout.SOUTH);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String task = taskField.getText().trim();
if (!task.isEmpty()) {
taskListModel.addElement(task);
taskField.setText("");
} else {
JOptionPane.showMessageDialog(frame, "Please enter a task!");
}
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
taskListModel.remove(selectedIndex);
} else {
JOptionPane.showMessageDialog(frame, "Please select a task to delete!");
}
}
});
// Show Frame
frame.setVisible(true);
}
}