Calendar Application
Calendar Application
import java.awt.*;
import java.time.*;
import java.time.format.TextStyle;
import java.util.*;
import javax.swing.*;
public class CalendarApp extends JFrame {
private JPanel calendarPanel;
private LocalDate currentDate;
private Map<LocalDate, String> notes;
private JComboBox<String> monthCombo;
private JComboBox<Integer> yearCombo;
public CalendarApp() {
setTitle("Colorful Calendar");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
notes = new HashMap<>();
currentDate = LocalDate.now().withDayOfMonth(1);
setLayout(new BorderLayout());
// Top Panel with Month and Year Selectors
JPanel topPanel = new JPanel();
monthCombo = new JComboBox<>();
for (Month m : Month.values()) {
monthCombo.addItem(m.getDisplayName(TextStyle.FULL, Locale.ENGLISH));
}
monthCombo.setSelectedIndex(currentDate.getMonthValue() - 1);
monthCombo.addActionListener(e -> updateDateFromSelection());
yearCombo = new JComboBox<>();
for (int i = 1970; i <= 2100; i++) {
yearCombo.addItem(i);
}
yearCombo.setSelectedItem(currentDate.getYear());
yearCombo.addActionListener(e -> updateDateFromSelection());
topPanel.add(new JLabel("Month:"));
topPanel.add(monthCombo);
topPanel.add(new JLabel("Year:"));
topPanel.add(yearCombo);
add(topPanel, BorderLayout.NORTH);
// Calendar Panel
calendarPanel = new JPanel();
calendarPanel.setLayout(new GridLayout(0, 7));
add(calendarPanel, BorderLayout.CENTER);
refreshCalendar();
setVisible(true);
}
private void updateDateFromSelection() {
int month = monthCombo.getSelectedIndex() + 1;
int year = (int) yearCombo.getSelectedItem();
currentDate = LocalDate.of(year, month, 1);
refreshCalendar();
}
private void refreshCalendar() {
calendarPanel.removeAll();
// Weekday headers (Starting from Monday)
DayOfWeek[] weekDays = {
DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY,
DayOfWeek.THURSDAY, DayOfWeek.FRIDAY, DayOfWeek.SATURDAY,
DayOfWeek.SUNDAY
};
for (DayOfWeek day : weekDays) {
JLabel lbl = new JLabel(day.getDisplayName(TextStyle.SHORT, Locale.ENGLISH), SwingConstants.CENTER);
lbl.setForeground(day == DayOfWeek.SUNDAY ? Color.RED : Color.BLUE);
calendarPanel.add(lbl);
}
// Dates
LocalDate firstOfMonth = currentDate.withDayOfMonth(1);
int startDay = firstOfMonth.getDayOfWeek().getValue() - 1; // Monday = 0
int daysInMonth = firstOfMonth.lengthOfMonth();
LocalDate today = LocalDate.now();
// Empty placeholders before the first day
for (int i = 0; i < startDay; i++) {
calendarPanel.add(new JLabel(""));
}
// Create buttons for each day
for (int day = 1; day <= daysInMonth; day++) {
LocalDate date = currentDate.withDayOfMonth(day);
JButton btn = new JButton(String.valueOf(day));
// Style for notes
if (notes.containsKey(date)) {
btn.setBackground(Color.RED);
btn.setForeground(Color.WHITE);
btn.setToolTipText(notes.get(date));
}
// Style for today's date
if (date.equals(today)) {
btn.setBackground(Color.GREEN);
btn.setForeground(Color.BLACK);
}
// Style Sundays with red text (unless already white from notes)
if (date.getDayOfWeek() == DayOfWeek.SUNDAY && !notes.containsKey(date)) {
btn.setForeground(Color.RED);
}
btn.addActionListener(e -> {
String dayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
String existingNote = notes.getOrDefault(date, "");
String input = JOptionPane.showInputDialog(this,
"Date: " + date + " (" + dayOfWeek + ")\nEnter Note:", existingNote);
if (input != null && !input.trim().isEmpty()) {
notes.put(date, input.trim());
} else {
notes.remove(date);
}
refreshCalendar(); // Update visuals
});
calendarPanel.add(btn);
}
calendarPanel.revalidate();
calendarPanel.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(CalendarApp::new);
}
}
OUTPUT :