-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoListAppGUI.java
More file actions
118 lines (106 loc) · 5.27 KB
/
TodoListAppGUI.java
File metadata and controls
118 lines (106 loc) · 5.27 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
package src;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class TodoListAppGUI {
private JFrame frame;
private JPanel taskListPanel;
private ArrayList<JPanel> taskItems;
public TodoListAppGUI() {
frame = new JFrame("Java To-Do List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 600);
frame.setMinimumSize(new Dimension(400, 400));
frame.setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
JLabel titleLabel = new JLabel("My To-Do List", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
mainPanel.add(titleLabel, BorderLayout.NORTH);
JPanel inputPanel = new JPanel(new BorderLayout(5, 5));
JTextField taskInputField = new JTextField();
taskInputField.setFont(new Font("Arial", Font.PLAIN, 16));
JButton addTaskButton = new JButton("Add Task");
addTaskButton.setFont(new Font("Arial", Font.BOLD, 14));
addTaskButton.setBackground(new Color(59, 130, 246)); // Tailwind's blue-500
addTaskButton.setForeground(Color.WHITE);
addTaskButton.setFocusPainted(false);
addTaskButton.setBorder(BorderFactory.createEmptyBorder(8, 16, 8, 16));
inputPanel.add(taskInputField, BorderLayout.CENTER);
inputPanel.add(addTaskButton, BorderLayout.EAST);
mainPanel.add(inputPanel, BorderLayout.SOUTH);
taskItems = new ArrayList<>();
taskListPanel = new JPanel();
taskListPanel.setLayout(new BoxLayout(taskListPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(taskListPanel);
scrollPane.setBorder(BorderFactory.createEmptyBorder());
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mainPanel.add(scrollPane, BorderLayout.CENTER);
ActionListener addTaskListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String taskText = taskInputField.getText().trim();
if (!taskText.isEmpty()) {
addTask(taskText);
taskInputField.setText("");
}
}
};
addTaskButton.addActionListener(addTaskListener);
taskInputField.addActionListener(addTaskListener);
frame.add(mainPanel);
frame.setVisible(true);
}
private void addTask(String taskText) {
JPanel taskPanel = new JPanel();
taskPanel.setLayout(new BorderLayout(5, 5));
taskPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, Color.lightGray),
BorderFactory.createEmptyBorder(10, 10, 10, 10)
));
JCheckBox completeCheckbox = new JCheckBox();
completeCheckbox.addActionListener(e -> {
JLabel label = (JLabel) taskPanel.getComponent(1);
if (completeCheckbox.isSelected()) {
label.setText("<html><strike>" + label.getText() + "</strike></html>");
label.setForeground(Color.GRAY);
} else {
String originalText = label.getText().replace("<html><strike>", "").replace("</strike></html>", "");
label.setText(originalText);
label.setForeground(Color.BLACK);
}
});
JLabel taskLabel = new JLabel(taskText);
taskLabel.setFont(new Font("Arial", Font.PLAIN, 16));
JButton deleteButton = new JButton("Delete");
deleteButton.setFont(new Font("Arial", Font.BOLD, 12));
deleteButton.setBackground(new Color(239, 68, 68)); // Tailwind's red-500
deleteButton.setForeground(Color.WHITE);
deleteButton.setFocusPainted(false);
deleteButton.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
deleteButton.addActionListener(e -> {
taskListPanel.remove(taskPanel);
taskItems.remove(taskPanel);
taskListPanel.revalidate();
taskListPanel.repaint();
});
taskPanel.add(completeCheckbox, BorderLayout.WEST);
taskPanel.add(taskLabel, BorderLayout.CENTER);
taskPanel.add(deleteButton, BorderLayout.EAST);
taskItems.add(taskPanel);
taskListPanel.add(taskPanel);
taskListPanel.revalidate();
taskListPanel.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TodoListAppGUI();
}
});
}
}