import javax.swing.
*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class ToDoListApp {
private JFrame frame;
private JTextField taskField;
private JList<String> taskList;
private DefaultListModel<String> listModel;
public ToDoListApp() {
frame = new JFrame("To-Do List App");
taskField = new JTextField(20);
JButton addButton = new JButton("Add Task");
JButton removeButton = new JButton("Remove Task");
listModel = new DefaultListModel();
taskList = new JList(listModel);
JScrollPane listScrollPane = new JScrollPane(taskList);
// Layout setup
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(taskField, BorderLayout.NORTH);
panel.add(listScrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
// Add action listeners
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String task = taskField.getText().trim();
if (!task.isEmpty()) {
listModel.addElement(task);
taskField.setText("");
} else {
JOptionPane.showMessageDialog(frame, "Please enter a task.");
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
listModel.remove(selectedIndex);
} else {
JOptionPane.showMessageDialog(frame, "Please select a task to remove.");
});
// Final setup
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(400, 300);
frame.setVisible(true);
frame.setLocationRelativeTo(null); // Center the window
public static void main(String[] args) {
SwingUtilities.invokeLater(ToDoListApp:new);
OUTPUT: