Google Advertisement

Simple Java To-Do App with Core Features (Add/Delete/Mark Complete)

Google Advertisement
πŸ”₯ Read with Full Features on Our Website

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.

Published on 04 May 2025
By Vandu

Create a To-Do List App with Add/Delete/Complete Features in Java

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.

πŸ”₯ Read with Full Features on Our Website

This beginner project will help you understand:

Let’s get started!


🧱 Tools & Prerequisites


🎯 Features of Our To-Do List App


πŸ§‘‍πŸ’» Step-by-Step Code with Explanation

πŸ“‚ Step 1: Create the Java Class

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);
    }
}

πŸ” Step-by-Step Explanation

1. Imports

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

2. Class Declaration

We extend JFrame to create a windowed app.

public class ToDoListApp extends JFrame {
 

3. UI Components

DefaultListModel<String> listModel;
JList<String> taskList;
JTextField taskInput;
ArrayList<Boolean> taskCompletionStatus;

4. Layout & Panels

We use BorderLayout for the main window and GridLayout for the buttons.

setLayout(new BorderLayout());
 

5. Add Task Action

When the user enters a task and clicks "Add", we:

addButton.addActionListener(e -> {
    String task = taskInput.getText().trim();
    ...
});
 

6. Delete Task Action

We delete the selected task from both the list and the status array.

deleteButton.addActionListener(e -> {
    int selectedIndex = taskList.getSelectedIndex();
    ...
});
 

7. Mark Task as Complete

We prepend a checkmark (βœ”οΈ) to completed tasks and mark them as true.

completeButton.addActionListener(e -> {
    int selectedIndex = taskList.getSelectedIndex();
    ...
});
 

πŸ§ͺ Output Preview

When you run the program:


βœ… Final Thoughts

This simple To-Do List App in Java is a great project for beginners to:

You can further enhance this app by:

❀️ Like πŸ’¬ Comment πŸ”— Share
Google Advertisement
πŸ‘‰ View Full Version on Main Website β†—
Google Advertisement
πŸ‘‰ Read Full Article on Website