简体   繁体   中英

How to disable or highlight the dates in JCalendar

In my case i want to disable or highlight dates in Java calendar. I used JCalendar and DateChooserCombo and could not find a way to do it. Finally, I tried the below code and it also was not successful.

For example: I want to disable all dates from 14-09-13 to 23-09-13 .

DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
try {
     Date d1 = formatter.parse("2013-09-14");
     Date d2 = formatter.parse("2013-09-23");
     jCalendar1.setSelectableDateRange(d1, d2);
 } catch (ParseException ex) {
    ex.printStackTrace();
 }

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 MinMaxDateEvaluator class, 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 RangeEvaluator class

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM