Java add icon to JCalendar

夙愿已清 提交于 2019-12-11 12:28:12

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/30696992/java-add-icon-to-jcalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!