问题
I want to disable multiple date ranges on a JCalendar. I'm following these steps, but I need to know how can I add multiple date evaluators. Help me please, thanks.
Update: I don't know why I can't disable the dates on my JCalendar. I will leave my code here so you guys can check it.
This is my RangeEvaluator class, modified from this class.
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();
}
@Override
public String getInvalidTooltip() {
return null;
}
@Override
public Color getInvalidBackroundColor() {
return null;
}
@Override
public Color getInvalidForegroundColor() {
return null;
}
}
Here is how I am using the RangeEvaluator class:
RangeEvaluator evaluator = new RangeEvaluator();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
evaluator.setStartDate(dateFormat.parse("11-09-2014"));
evaluator.setEndDate(dateFormat.parse("15-09-2014"));
jCalendar1.getDayChooser().addDateEvaluator(evaluator);
Am I missing something?. Help me please, Thanks.
回答1:
Based on your update there are two things that happen here.
First is a little mistake when you instatiate your SimpleDateFormat:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
In this pattern "mm" refers to minutes not months. It should be:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Second problem is IMHO a bug since day chooser seems to not apply filters until a new date is set or it has to be repainted because of month/year change. You can test it changing month to August and then back to September: dates from 9/11 to 9/15 will be disabled. To solve this inconvenient behavior just set current date explicitely like this:
JCalendar calendar = new JCalendar();
calendar.getDayChooser().addDateEvaluator(evaluator);
calendar.setDate(Calendar.getInstance().getTime());
Side note: while this library is very very useful it has a couple of bug/design issues so don't hesitate to ask for help.
回答2:
Just call addDateEvaluator()
for each RangeEvaluator
that you create. This adds the RangeEvaluator
to a List dateEvaluators
inside JDayChooser
. Later, JDayChooser
iterates over the list when it decides how to draw the day buttons.
Edit: Here's the RangeEvaluator
I used.
private static class RangeEvaluator extends MinMaxDateEvaluator {
@Override
public boolean isInvalid(Date date) {
return !super.isInvalid(date);
}
}
And here's how I used it.
RangeEvaluator re = new RangeEvaluator();
re.setMinSelectableDate(...);
re.setMaxSelectableDate(...);
JCalendar jc = new JCalendar();
jc.getDayChooser().addDateEvaluator(re);
One problem I noticed is that you have to tell the JDayChooser
to reconfigure its buttons using the new evaluator. You can fire a property change event or just change a bound property.
jc.setCalendar(jc.getCalendar());
来源:https://stackoverflow.com/questions/25501373/disable-multiple-date-ranges-jdatechooser