How to disable or highlight the dates in JCalendar

◇◆丶佛笑我妖孽 提交于 2019-11-27 14:54:48

I know this has been inactive for a while but hopefully it can be useful to someone. The key here is implement IDateEvaluator interface which is intended to validate if a date is special or invalid. Unfortunately there's only one concrete implementation provided with JCalendar library which is MinMaxDateEvaluatorclass, but taking this as start point is not so complicated.

RangeEvaluator

Here is an example of implementation, please pay special attention to isInvalid(Date date) method. Also you may want to look at DateUtil class which is also part of JCalendar library.

class RangeEvaluator implements IDateEvaluator {

    private DateUtil dateUtil = new DateUtil();

    @Override
    public boolean isSpecial(Date date) {
        return false;
    }

    @Override
    public Color getSpecialForegroundColor() {
        return null;
    }

    @Override
    public Color getSpecialBackroundColor() {
        return null;
    }

    @Override
    public String getSpecialTooltip() {
        return null;
    }
    @Override
    public boolean isInvalid(Date date) {
        return dateUtil.checkDate(date);
        // if the given date is in the range then is invalid
    }        

    /**
     * Sets the initial date in the range to be validated.
     * @param startDate 
     */
    public void setStartDate(Date startDate) {
        dateUtil.setMinSelectableDate(startDate);
    }

    /**
     * @return the initial date in the range to be validated.
     */
    public Date getStartDate() {
        return dateUtil.getMinSelectableDate();
    }

    /**
     * Sets the final date in the range to be validated.
     * @param endDate 
     */
    public void setEndDate(Date endDate) {
        dateUtil.setMaxSelectableDate(endDate);
    }

    /**
     * @return the final date in the range to be validated.
     */
    public Date getEndDate() {
        return dateUtil.getMaxSelectableDate();
    }        
}

Using RangeEvaluatorclass

There is an example of using RangeEvaluator class below. Please note the range from September 14th to September 23rd is disabled.

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

RangeEvaluator evaluator = new RangeEvaluator();
evaluator.setStartDate(dateFormat.parse("2013-09-14"));
evaluator.setEndDate(dateFormat.parse("2013-09-23"));

JCalendar calendar = new JCalendar(Locale.US);
calendar.getDayChooser().addDateEvaluator(evaluator); // evaluator must be added to a JDayChooser object

Screenshot

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