Disabling dates in a JXDatePicker/JXMonthView

2019-05-20 23:19发布

问题:

A program I'm writing allows users to click a date on a JXDatePicker to set the date a task has been completed. I would like to disable the selection of future dates in the JXDatePicker, since in my program selecting future dates is invalid.

I have found that JXDatePickers contain a JXMonthView, and it does not seem that date pickers or month views allow you to disable individual/ranges of dates. I can change the background coloring of individual dates and ranges of dates, which should allow me to make future dates a separate color. Then I could add in some validation whenever the user clicks the calendar to disallow future dates. However, it would be so much cleaner if I could just say something like calendar.setMaxDate(today);

Does anyone know if there is an easier method of doing this than hand-coding the functionality? Perhaps another date picker component fixes this problem?

回答1:

If you want to restrict date selection to only a range, you can specify upper and lower boundries

JXDatePicker picker = new JXDatePicker();
Calendar calendar = picker.getMonthView().getCalendar();
// starting today if we are in a hurry
calendar.setTime(new Date());
picker.getMonthView().setLowerBound(calendar.getTime());
// end of next week
CalendarUtils.endOfWeek(calendar);
calendar.add(Calendar.WEEK_OF_YEAR);
picker.getMonthView().setUpperBound(calendar.getTime());

(Taken from the JavaDocs)

This will basically restrict the valid, selectable dates from today to the end of the week.



回答2:

After a few minutes of research, it looks like JDatePicker will work here - see http://jdatepicker.com/tour-date-restriction.html

Still open to suggestions on accomplishing this using JXDatePickers though, if anyone is aware of a simple method of doing so.



回答3:

package org.chillies.validator;
import java.util.Date;
@SuppressWarnings("deprecation")
public class DateValidator {
public Date getlowerBoundDate(){
    String date;
    Date date1=new Date();
    Integer year=1900+date1.getYear();
    date="01-Apr-"+year;
    return new Date(date);
}
public Date getupperBoundDate(){
    String date;
    Date date1=new Date();
    Integer year=1900+date1.getYear();
    date="31-Mar-"+(year+1);//you can set date as you wish over here
    return new Date(date);
 }
}

rest of the code is like this to apply this class

datePicker.setBorder(null);
    DateValidator datevalidator=new DateValidator();
    datePicker.getMonthView().setLowerBound(datevalidator.getlowerBoundDate());
    datePicker.getMonthView().setUpperBound(datevalidator.getupperBoundDate());


回答4:

        Callback<DatePicker, DateCell> callB = new Callback<DatePicker, DateCell>() {
            @Override
            public DateCell call(final DatePicker param) {
                return new DateCell() {
                    @Override
                    public void updateItem(LocalDate item, boolean empty) {
                        super.updateItem(item, empty); //To change body of generated methods, choose Tools | Templates.
                        LocalDate today = LocalDate.now();
                        setDisable(empty || item.compareTo(today) < 0);
                    }

                };
            }

        };
        selectedDate.setDayCellFactory(callB);

date disable.......