Bitcoin

Bitcoin
Bitcoin

PROFESSIONAL DAILY ACTIVITIES MANAGEMENT APPLICATION

 We'll make a professional daily activities management application using a clickable calendar (date picker) instead of typing the date manually.

For pure Java Swing, we can use JDateChooser from JCalendar (a small open-source library).


---


Steps to Add Date Picker


1. Download JCalendar Library


Get JCalendar.jar and add it to your project classpath.


2. Use JDateChooser instead of a simple text field.


---


Updated Code with Calendar


import com.toedter.calendar.JDateChooser;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.SimpleDateFormat;

import java.util.*;

import java.util.List;

import java.util.Timer;

import java.util.TimerTask;


public class DailyActivityCoordinatorCalendar extends JFrame {

    private JTextField activityField, timeField;

    private JDateChooser dateChooser;

    private DefaultListModel<String> activityListModel;

    private List<Activity> activityListData = new ArrayList<>();

    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");


    public DailyActivityCoordinatorCalendar() {

        setTitle("Daily Activity Coordinator with Calendar");

        setSize(500, 350);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLocationRelativeTo(null);


        // Components

        JLabel activityLabel = new JLabel("Activity:");

        activityField = new JTextField(10);


        JLabel timeLabel = new JLabel("Time (HH:mm):");

        timeField = new JTextField(6);


        JLabel dateLabel = new JLabel("Pick Date:");

        dateChooser = new JDateChooser();

        dateChooser.setDateFormatString("dd-MM-yyyy");


        JButton addButton = new JButton("Add Activity");

        JButton clearButton = new JButton("Clear All");


        activityListModel = new DefaultListModel<>();

        JList<String> activityList = new JList<>(activityListModel);


        // Input panel

        JPanel inputPanel = new JPanel();

        inputPanel.add(activityLabel);

        inputPanel.add(activityField);

        inputPanel.add(timeLabel);

        inputPanel.add(timeField);

        inputPanel.add(dateLabel);

        inputPanel.add(dateChooser);

        inputPanel.add(addButton);

        inputPanel.add(clearButton);


        add(inputPanel, BorderLayout.NORTH);

        add(new JScrollPane(activityList), BorderLayout.CENTER);


        // Button listeners

        addButton.addActionListener(e -> addActivity());

        clearButton.addActionListener(e -> clearActivities());


        // Timer for alarm

        Timer timer = new Timer(true);

        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {

                checkAlarms();

            }

        }, 0, 60000); // check every minute

    }


    private void addActivity() {

        try {

            String activity = activityField.getText().trim();

            Date selectedDate = dateChooser.getDate();

            Date time = timeFormat.parse(timeField.getText().trim());


            if (!activity.isEmpty() && selectedDate != null) {

                activityListModel.addElement(dateFormat.format(selectedDate) + " " +

                        timeField.getText() + " - " + activity);

                activityListData.add(new Activity(activity, selectedDate, time));

                activityField.setText("");

                timeField.setText("");

            } else {

                JOptionPane.showMessageDialog(this, "Enter activity and pick date!");

            }

        } catch (Exception ex) {

            JOptionPane.showMessageDialog(this, "Invalid time format (HH:mm)");

        }

    }


    private void clearActivities() {

        activityListModel.clear();

        activityListData.clear();

    }


    private void checkAlarms() {

        Date now = new Date();

        String today = dateFormat.format(now);

        String currentTime = timeFormat.format(now);


        for (Activity a : activityListData) {

            if (dateFormat.format(a.date).equals(today) &&

                timeFormat.format(a.time).equals(currentTime)) {

                SwingUtilities.invokeLater(() -> {

                    JOptionPane.showMessageDialog(this,

                            "Reminder: " + a.name + " at " + currentTime);

                });

            }

        }

    }


    class Activity {

        String name;

        Date date;

        Date time;

        Activity(String name, Date date, Date time) {

            this.name = name;

            this.date = date;

            this.time = time;

        }

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new DailyActivityCoordinatorCalendar().setVisible(true));

    }

}


Added JDateChooser for a professional date picker.


No need to type date manually anymore.


Rest of the features (alarm notifications, clear, list display) remain.


#followers #highlights

No comments:

Post a Comment

Facebook