Learn how to build a basic to-do list app in Java with add, delete, and mark-as-complete functionalities. Beginner-friendly tutorial with clear code explanations.
Managing tasks is a daily routine for many of us, and a to-do list app helps us stay organized. In this step-by-step tutorial, we will create a To-Do List App in Java using Swing — a GUI toolkit for building desktop apps.
This beginner project will help you understand:
GUI design using Swing
List data management
Event handling (buttons and actions)
Let’s get started!
JDK (Java Development Kit) installed
Any IDE like IntelliJ IDEA, Eclipse, or NetBeans
Basic knowledge of Java syntax and OOP
Add new tasks
Delete selected tasks
Mark tasks as completed
View tasks in a scrollable list
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class ToDoListApp extends JFrame {
private DefaultListModel<String> listModel;
private JList<String> taskList;
private JTextField taskInput;
private ArrayList<Boolean> taskCompletionStatus;
public ToDoListApp() {
setTitle("To-Do List App");
setSize(400, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
listModel = new DefaultListModel<>();
taskList = new JList<>(listModel);
taskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(taskList);
taskInput = new JTextField();
JButton addButton = new JButton("Add Task");
JButton deleteButton = new JButton("Delete Task");
JButton completeButton = new JButton("Mark as Complete");
taskCompletionStatus = new ArrayList<>();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 3));
panel.add(addButton);
panel.add(deleteButton);
panel.add(completeButton);
add(taskInput, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
// Action to add a task
addButton.addActionListener(e -> {
String task = taskInput.getText().trim();
if (!task.isEmpty()) {
listModel.addElement(task);
taskCompletionStatus.add(false);
taskInput.setText("");
}
});
// Action to delete a task
deleteButton.addActionListener(e -> {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
listModel.remove(selectedIndex);
taskCompletionStatus.remove(selectedIndex);
}
});
// Action to mark as complete
completeButton.addActionListener(e -> {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
boolean isCompleted = taskCompletionStatus.get(selectedIndex);
if (!isCompleted) {
String task = listModel.getElementAt(selectedIndex);
task = "✔️ " + task;
listModel.set(selectedIndex, task);
taskCompletionStatus.set(selectedIndex, true);
}
}
});
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ToDoListApp::new);
}
}
import javax.swing.*; // for GUI components
import java.awt.*; // for layout
import java.awt.event.*; // for button actions
import java.util.ArrayList; // to track completion status
We extend JFrame
to create a windowed app.
public class ToDoListApp extends JFrame {
DefaultListModel<String>
: Holds the list of tasks.
JList<String>
: Displays the list to users.
JTextField
: For inputting new tasks.
JButtons
: To add, delete, and mark tasks.
DefaultListModel<String> listModel;
JList<String> taskList;
JTextField taskInput;
ArrayList<Boolean> taskCompletionStatus;
We use BorderLayout
for the main window and GridLayout
for the buttons.
setLayout(new BorderLayout());
When the user enters a task and clicks "Add", we:
Add it to the listModel
Mark its completion as false
Clear the input field
addButton.addActionListener(e -> {
String task = taskInput.getText().trim();
...
});
We delete the selected task from both the list and the status array.
deleteButton.addActionListener(e -> {
int selectedIndex = taskList.getSelectedIndex();
...
});
We prepend a checkmark (✔️) to completed tasks and mark them as true
.
completeButton.addActionListener(e -> {
int selectedIndex = taskList.getSelectedIndex();
...
});
When you run the program:
A window opens with a text field and three buttons.
You can enter tasks, select them, and either delete or mark them as complete.
This simple To-Do List App in Java is a great project for beginners to:
Practice Java Swing
Understand event-driven programming
Learn how to manipulate lists dynamically
You can further enhance this app by:
Saving tasks to a file
Adding deadlines or categories
Creating a dark/light theme
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!