Skip to content

🚀 “This Simple Java App Will Blow Your Mind! (Built With Swing 🔥)”

🧠 What You’ll Learn

If you’re a beginner or intermediate Java programmer, this mini-project is your golden ticket 🎟️ to mastering:

  • GUI development with Swing
  • Event handling
  • Object-oriented design
  • Managing tasks in a real-world app

Let’s build a To-Do List App that lets users: ✅ Add tasks
✅ Mark them as completed
✅ Delete tasks
✅ All using Java Swing — no external libraries!

🛠️ Project Overview

FeatureTech Used
GUIJava Swing
Task ManagementArrayList
Display ListJList + DefaultListModel

📁 File Structure

We’ll use just one file for simplicity:

TaskManagerApp.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

class Task {
    private String description;
    private boolean completed;

    public Task(String description) {
        this.description = description;
        this.completed = false;
    }

    public String getDescription() {
        return description;
    }

    public boolean isCompleted() {
        return completed;
    }

    public void toggleCompleted() {
        completed = !completed;
    }

    @Override
    public String toString() {
        return (completed ? "[✓] " : "[ ] ") + description;
    }
}

public class TaskManagerApp extends JFrame {
    private final DefaultListModel<String> listModel = new DefaultListModel<>();
    private final JList<String> taskJList = new JList<>(listModel);
    private final ArrayList<Task> tasks = new ArrayList<>();

    public TaskManagerApp() {
        setTitle("To-Do List App");
        setSize(400, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JTextField taskInput = new JTextField(20);
        JButton addButton = new JButton("Add");
        JButton markDoneButton = new JButton("Mark Done");
        JButton deleteButton = new JButton("Delete");

        JPanel inputPanel = new JPanel();
        inputPanel.add(taskInput);
        inputPanel.add(addButton);

        JPanel actionPanel = new JPanel();
        actionPanel.add(markDoneButton);
        actionPanel.add(deleteButton);

        add(new JScrollPane(taskJList), BorderLayout.CENTER);
        add(inputPanel, BorderLayout.NORTH);
        add(actionPanel, BorderLayout.SOUTH);

        addButton.addActionListener(e -> {
            String text = taskInput.getText().trim();
            if (!text.isEmpty()) {
                Task newTask = new Task(text);
                tasks.add(newTask);
                listModel.addElement(newTask.toString());
                taskInput.setText("");
            }
        });

        markDoneButton.addActionListener(e -> {
            int index = taskJList.getSelectedIndex();
            if (index != -1) {
                tasks.get(index).toggleCompleted();
                listModel.set(index, tasks.get(index).toString());
            }
        });

        deleteButton.addActionListener(e -> {
            int index = taskJList.getSelectedIndex();
            if (index != -1) {
                tasks.remove(index);
                listModel.remove(index);
            }
        });

        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TaskManagerApp::new);
    }
}

🧠 Code Breakdown (Line-by-Line)

🔹 Task Class

class Task {
    private String description;
    private boolean completed;

Creates a blueprint for tasks with a description and completed status.

public Task(String description) {
        this.description = description;
        this.completed = false;
    }

Initializes new tasks as incomplete.

public void toggleCompleted() {
        completed = !completed;
    }

Switches the status between done and not done.

    @Override
    public String toString() {
        return (completed ? "[✓] " : "[ ] ") + description;
    }

Nicely formats the task for the UI.

🔹 TaskManagerApp Class (GUI)

public class TaskManagerApp extends JFrame {

The main window for our application.

    private final DefaultListModel<String> listModel = new DefaultListModel<>();
    private final JList<String> taskJList = new JList<>(listModel);
    private final ArrayList<Task> tasks = new ArrayList<>();

🔹 Inside the Constructor

        setTitle("To-Do List App");
        setSize(400, 500);

Window title and size.

        JTextField taskInput = new JTextField(20);
        JButton addButton = new JButton("Add");

UI components for task input and adding.

        add(new JScrollPane(taskJList), BorderLayout.CENTER);
        add(inputPanel, BorderLayout.NORTH);

Places the components using layout regions.


🔹 Button Logic

➕ Add Button

        addButton.addActionListener(e -> {
            ...
        });

Adds task to the list and updates the UI.

✅ Mark Done

        markDoneButton.addActionListener(e -> {
            ...
        });

Toggles task completion visually and logically.

❌ Delete

        deleteButton.addActionListener(e -> {
            ...
        });

Removes the selected task from the list.


🔚 Main Method

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TaskManagerApp::new);
    }

Launches the app in a thread-safe way for Swing.


🧪 How to Run

javac TaskManagerApp.java
java TaskManagerApp

No JavaFX, no Maven — just raw Java and Swing. Simple and portable.


💡 Bonus Ideas to Level Up

  • Save/load tasks using a text file
  • Add due dates and categories
  • Light/Dark theme toggle
  • Drag-and-drop reordering

✍️ Final Thoughts

This mini Java project might look small, but it teaches a ton — GUI design, event handling, object management, and more. And the best part? It’s 100% vanilla Java — perfect for learning and building cool things without relying on big frameworks.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *