0% found this document useful (0 votes)
1 views

code

The document presents a Java Swing application for a To-Do List. It includes functionalities to add and remove tasks through a graphical user interface. The application features a text field for input, buttons for task management, and a list to display tasks.

Uploaded by

shabana bardol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

code

The document presents a Java Swing application for a To-Do List. It includes functionalities to add and remove tasks through a graphical user interface. The application features a text field for input, buttons for task management, and a list to display tasks.

Uploaded by

shabana bardol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

You might also like