如何在JCalendar中禁用或突出显示日期 [英] How to disable or highlight the dates in JCalendar

查看:172
本文介绍了如何在JCalendar中禁用或突出显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的情况下,我想在Java日历中禁用或突出显示日期。我使用 JCalendar DateChooserCombo ,但找不到办法。最后,我尝试了下面的代码,但也没有成功。

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.

例如:我想禁用从 14-09-13 的所有日期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();
 }


推荐答案

我知道这一直无效一段时间但希望它对某人有用。这里的关键是实现 IDateEvaluator 接口,该接口用于验证日期是特殊还是无效。不幸的是, JCalendar 库只提供了一个具体的实现,它是 MinMaxDateEvaluator class,但以此为起点并不复杂。

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.

下面是一个实现示例,请特别注意 isInvalid(Date date)方法。您还可以查看 DateUtil 类,它也是 JCalendar 库的一部分。

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



使用 RangeEvaluator class



下面有一个使用 RangeEvaluator 类的例子。请注意,9月14日至9月23日的范围被禁用。

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

这篇关于如何在JCalendar中禁用或突出显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆