简体   繁体   中英

How to highlight dates inside JCalendar in JDateChooser?

I am using a JDateChooser component from an external library toedter(jcalendar1.4). I used a custom DateEvaluator by implementing IDateEvaluator which is later added to JDateChooser component:

public static class HighlightEvaluator implements IDateEvaluator {
    private final List<Date> list = new ArrayList<>();
    public void add(Date date) {
        list.add(date);
    }

    public void remove(Date date) {
        list.remove(date);
    }

    public void setDates(List<Date> dates) {
        list.addAll(dates);
    }

    @Override
    public Color getInvalidBackroundColor() {
        return Color.BLACK;
    }

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

    @Override
    public String getInvalidTooltip() {
        return null;
    }

    @Override
    public Color getSpecialBackroundColor() {
        return Color.GREEN;
    }

    @Override
    public Color getSpecialForegroundColor() {
        return Color.RED;
    }

    @Override
    public String getSpecialTooltip() {
        return "filled";
    }

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

    @Override
    public boolean isSpecial(Date date) {
        return list.contains(date);
    }
}

and here is my main method:

public static void main(){
JDateChooser dateChooser = new JDateChooser();    
List<Date> dates = new ArrayList<Date>();
dates.add(new Date());
HighlightEvaluator evaluator = new HighlightEvaluator();
evaluator.setDates(dates);
dateChooser.getJCalendar().getDayChooser().addDateEvaluator(evaluator);
}

Now the current date is supposed to be highlighted by the code but its not getting highlighted. Please tell a fix for this

You need to alter the Date variable a little bit. When you create a Date variable it grabs the current day month year along with time(hours, minutes, seconds, milliseconds)

However setting the same date to evaluator will not work because evaluator expects date to be devoid of the time details. So alternatively you can use Calendar object in your main function as illustrated below:

public static void main(){
JDateChooser dateChooser = new JDateChooser();    
List<Date> dates = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, desiredyear);
c.set(Calendar.MONTH, desiredmonth);
c.set(Calendar.DAY_OF_MONTH, desiredday);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
dates.add(c.getTime());
HighlightEvaluator evaluator = new HighlightEvaluator();
evaluator.setDates(dates);    
dateChooser.getJCalendar().getDayChooser().addDateEvaluator(evaluator);
}

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