Search code examples
javaswingimageiconjcalendar

Java add icon to JCalendar


I'm trying to add an icon to a specific day into a JCalendar, but I can't.

How can I do that?

I have this code:

final JCalendar calendar = new JCalendar();
    JDayChooser day= calendar.getDayChooser();
    day.setAlwaysFireDayProperty(true);
    day.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            //put icon here
            ImageIcon icon = new ImageIcon("icon.png");
            JLabel label = new JLabel(icon);
            day.add(label);

        }

Edit: I want the icon in a day.


Solution

  • Adding an icon to the buttons of a JDayChooser is not supported. You'd have to extend JDayChooser and modify one of the buttons in the protected array named days. As the panel is already fairly crowded, I'm not sure the effect would be appealing.

    Alternatively, implement the IDateEvaluator interface and alter the colors for your chosen date, as shown here, here and other implementing classes in the distribution; the class com.toedter.calendar.demo.BirthdayEvaluator illustrates the approach.

    public boolean isSpecial(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) == yourSpecialMonth
        && calendar.get(Calendar.DAY_OF_MONTH) == yourSpecialDay;
    }
    
    public Color getSpecialForegroundColor() {
        return yourSpecialForegroundColor;
    }
    
    public Color getSpecialBackroundColor() {
        return yourSpecialBackroundColor;
    }