Disabling dates in a JXDatePicker/JXMonthView

余生颓废 提交于 2019-12-06 06:37:28

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.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!