0% found this document useful (0 votes)
2 views4 pages

Java To-Do List Code

The document describes a Java GUI application for managing a to-do list, allowing users to add, edit, delete, and filter tasks based on priority and due dates. It utilizes Swing components such as JTable for displaying tasks and JComboBox for selecting priorities and filters. The application handles task data through a custom Task class and provides functionality for user interaction through buttons and input fields.

Uploaded by

Kunal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Java To-Do List Code

The document describes a Java GUI application for managing a to-do list, allowing users to add, edit, delete, and filter tasks based on priority and due dates. It utilizes Swing components such as JTable for displaying tasks and JComboBox for selecting priorities and filters. The application handles task data through a custom Task class and provides functionality for user interaction through buttons and input fields.

Uploaded by

Kunal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab 8 - To Do List Application (Java GUI)

import [Link].*;
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];

class Task {
String description;
String priority;
LocalDate dueDate;

Task(String d, String p, LocalDate date) {


description = d;
priority = p;
dueDate = date;
}
}

public class ToDoListApp extends JFrame {


private ArrayList<Task> tasks = new ArrayList<>();
private DefaultTableModel model;
private JTable table;
private JTextField descField, dateField;
private JComboBox<String> priorityBox, filterBox;

public ToDoListApp() {
setTitle("To-Do List Application");
setSize(700, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

model = new DefaultTableModel(new String[]{"Description", "Priority", "Due Date"}, 0);


table = new JTable(model);
add(new JScrollPane(table), [Link]);

JPanel inputPanel = new JPanel();


[Link](new GridLayout(2, 4, 10, 10));
descField = new JTextField();
priorityBox = new JComboBox<>(new String[]{"High", "Medium", "Low"});
dateField = new JTextField("yyyy-mm-dd");

JButton addBtn = new JButton("Add");


JButton editBtn = new JButton("Edit");
JButton delBtn = new JButton("Delete");

[Link](new JLabel("Description"));
[Link](new JLabel("Priority"));
[Link](new JLabel("Due Date"));
[Link](new JLabel("Actions"));

[Link](descField);
[Link](priorityBox);
[Link](dateField);

JPanel btnPanel = new JPanel();


[Link](addBtn);
[Link](editBtn);
[Link](delBtn);
[Link](btnPanel);

add(inputPanel, [Link]);

JPanel filterPanel = new JPanel();


filterBox = new JComboBox<>(new String[]{"All", "High", "Medium", "Low", "Today",
"This Week", "Overdue"});
JButton filterBtn = new JButton("Filter");
[Link](new JLabel("Filter By:"));
[Link](filterBox);
[Link](filterBtn);
add(filterPanel, [Link]);

[Link](e -> addTask());


[Link](e -> editTask());
[Link](e -> deleteTask());
[Link](e -> filterTasks());

setVisible(true);
}

private void addTask() {


String desc = [Link]();
String pr = (String) [Link]();
LocalDate date;
try {
date = [Link]([Link](), [Link]("yyyy-MM-
dd"));
} catch (Exception e) {
[Link](this, "Invalid Date Format!");
return;
}
[Link](new Task(desc, pr, date));
refreshTable(tasks);
clearFields();
}

private void editTask() {


int i = [Link]();
if (i == -1) return;
String desc = [Link]();
String pr = (String) [Link]();
LocalDate date;
try {
date = [Link]([Link](), [Link]("yyyy-MM-
dd"));
} catch (Exception e) {
[Link](this, "Invalid Date Format!");
return;
}
[Link](i, new Task(desc, pr, date));
refreshTable(tasks);
clearFields();
}

private void deleteTask() {


int i = [Link]();
if (i != -1) {
[Link](i);
refreshTable(tasks);
}
}

private void filterTasks() {


String choice = (String) [Link]();
LocalDate today = [Link]();
ArrayList<Task> filtered = new ArrayList<>();

for (Task t : tasks) {


if ([Link]("All")) [Link](t);
else if ([Link]("High") && [Link]("High")) [Link](t);
else if ([Link]("Medium") && [Link]("Medium")) [Link](t);
else if ([Link]("Low") && [Link]("Low")) [Link](t);
else if ([Link]("Today") && [Link](today)) [Link](t);
else if ([Link]("This Week") && ![Link](today) && !
[Link]([Link](7))) [Link](t);
else if ([Link]("Overdue") && [Link](today)) [Link](t);
}
refreshTable(filtered);
}

private void refreshTable(ArrayList<Task> list) {


[Link](0);
for (Task t : list) {
[Link](new Object[]{[Link], [Link], [Link]()});
}
}

private void clearFields() {


[Link]("");
[Link]("yyyy-mm-dd");
[Link](0);
}

public static void main(String[] args) {


[Link](ToDoListApp::new);
}
}

You might also like