Disabling dates in a JXDatePicker/JXMonthView

﹥>﹥吖頭↗ 提交于 2019-12-22 20:06:29

问题


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.......



来源:https://stackoverflow.com/questions/13226138/disabling-dates-in-a-jxdatepicker-jxmonthview

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