Group 1 Java
Group 1 Java
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class ColorfulCalendarApp {
private JFrame mainFrame;
private JLabel monthLabel;
private JPanel calendarPanel;
private JTextArea notesArea;
private JTextField dateInputField;
private Calendar currentCalendar;
private Map<String, String> dateNotes = new HashMap<>();
private String selectedDateKey = null; // Track the last selected date
public ColorfulCalendarApp() {
prepareGUI();
currentCalendar = Calendar.getInstance();
updateCalendar();
}
private void prepareGUI() {
mainFrame = new JFrame("Colorful Calendar Application");
mainFrame.setSize(800, 600);
mainFrame.setLayout(new BorderLayout());
mainFrame.getContentPane().setBackground(new Color(240, 240, 240));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setBackground(new Color(70, 130, 180));
topPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JButton prevButton = new JButton("←");
prevButton.setToolTipText("Go to the previous month");
prevButton.addActionListener(e -> {
currentCalendar.add(Calendar.MONTH, -1);
updateCalendar();
});
prevButton.setBackground(new Color(100, 149, 237));
prevButton.setForeground(Color.BLACK);
monthLabel = new JLabel("", SwingConstants.CENTER);
monthLabel.setFont(new Font("SansSerif", Font.BOLD, 20));
monthLabel.setForeground(Color.WHITE);
JButton nextButton = new JButton("→");
nextButton.setToolTipText("Go to the next month");
nextButton.addActionListener(e -> {
currentCalendar.add(Calendar.MONTH, 1);
updateCalendar();
});
nextButton.setBackground(new Color(100, 149, 237));
nextButton.setForeground(Color.BLACK);
topPanel.add(prevButton, BorderLayout.WEST);
topPanel.add(monthLabel, BorderLayout.CENTER);
topPanel.add(nextButton, BorderLayout.EAST);
calendarPanel = new JPanel(new GridLayout(0, 7));
calendarPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel lookupPanel = new JPanel();
lookupPanel.add(new JLabel("Find day for date (dd/mm/yyyy):"));
dateInputField = new JTextField(10);
lookupPanel.add(dateInputField);
JButton findButton = new JButton("Find");
findButton.setToolTipText("Find the day of the week for the entered date");
findButton.addActionListener(e -> findDayForDate());
lookupPanel.add(findButton);
bottomPanel.add(lookupPanel, BorderLayout.NORTH);
notesArea = new JTextArea(5, 20);
notesArea.setLineWrap(true);
notesArea.setWrapStyleWord(true);
notesArea.setBorder(BorderFactory.createTitledBorder("Notes for selected date"));
JScrollPane notesScrollPane = new JScrollPane(notesArea);
bottomPanel.add(notesScrollPane, BorderLayout.CENTER);
JButton saveNoteButton = new JButton("Save Note");
saveNoteButton.setToolTipText("Save a note for the selected date");
saveNoteButton.addActionListener(e -> saveNoteForSelectedDate());
bottomPanel.add(saveNoteButton, BorderLayout.SOUTH);
mainFrame.add(topPanel, BorderLayout.NORTH);
mainFrame.add(calendarPanel, BorderLayout.CENTER);
mainFrame.add(bottomPanel, BorderLayout.SOUTH);
mainFrame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
currentCalendar.add(Calendar.MONTH, -1);
updateCalendar();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
currentCalendar.add(Calendar.MONTH, 1);
updateCalendar();
}
}
});
mainFrame.setFocusable(true);
}
private void updateCalendar() {
calendarPanel.removeAll();
String month = currentCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG,
java.util.Locale.getDefault());
int year = currentCalendar.get(Calendar.YEAR);
monthLabel.setText(month + " " + year);
String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for (String day : days) {
JLabel label = new JLabel(day, SwingConstants.CENTER);
label.setBackground(new Color(220, 220, 220));
label.setOpaque(true);
calendarPanel.add(label);
}
Calendar tempCalendar = (Calendar) currentCalendar.clone();
tempCalendar.set(Calendar.DAY_OF_MONTH, 1);
int firstDayOfWeek = tempCalendar.get(Calendar.DAY_OF_WEEK);
int daysInMonth = tempCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i < firstDayOfWeek; i++) {
calendarPanel.add(new JLabel(""));
}
for (int day = 1; day <= daysInMonth; day++) {
final int currentDay = day;
JButton dayButton = new JButton(String.valueOf(day));
dayButton.setToolTipText("Click to view or add notes for this date");
dayButton.addActionListener(e -> showDateDetails(currentDay));
calendarPanel.add(dayButton);
}
calendarPanel.revalidate();
calendarPanel.repaint();
}
// Modified: No popup, just load note for the selected date
private void showDateDetails(int day) {
int month = currentCalendar.get(Calendar.MONTH) + 1;
int year = currentCalendar.get(Calendar.YEAR);
selectedDateKey = day + "/" + month + "/" + year; // Track selected date
notesArea.setText(dateNotes.getOrDefault(selectedDateKey, ""));
}
// Modified: Save note for the last selected date directly
private void saveNoteForSelectedDate() {
String note = notesArea.getText().trim();
if (selectedDateKey == null) {
JOptionPane.showMessageDialog(mainFrame, "Please select a date first.");
return;
}
if (!note.isEmpty()) {
dateNotes.put(selectedDateKey, note);
updateCalendar();
JOptionPane.showMessageDialog(mainFrame, "Note saved for " + selectedDateKey);
} else {
JOptionPane.showMessageDialog(mainFrame, "Please enter a note to save");
}
}
private void findDayForDate() {
String dateStr = dateInputField.getText().trim();
try {
String[] parts = dateStr.split("/");
if (parts.length != 3) throw new IllegalArgumentException();
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]) - 1;
int year = Integer.parseInt(parts[2]);
Calendar tempCalendar = Calendar.getInstance();
tempCalendar.set(year, month, day);
String dayOfWeek = tempCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG,
java.util.Locale.getDefault());
JOptionPane.showMessageDialog(mainFrame,
"Date: " + dateStr + "\n" +
"Day: " + dayOfWeek,
"Day Finder", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(mainFrame,
"Invalid date format. Please use dd/mm/yyyy",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
ColorfulCalendarApp app = new ColorfulCalendarApp();
app.mainFrame.setVisible(true);
});
}
}
OUTPUT :