Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

java - How to show only date after the date of today in JCalendar

i'm trying to limit user to select only the date after today, or select date after another Date I see on JCalendar API something that could help me but i didn't find nothing.. how can i do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do not think there is a straight forward way on the component to do this. One way, that I know of is to use the setSelectableDateRange(Date from,Date to) - When you set the from date to current date, all previous day cells, year/month drop downs becomes disabled.

Example:

    JCalendar calendar = new JCalendar();
    calendar.setSelectableDateRange(new Date(),new SimpleDateFormat("MM-DD-YYYY").parse("05-05-2015"));

    PropertyChangeListener calendarChangeListener  = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Date selectedDate = ((JCalendar)evt.getSource()).getDate();
        }
    };
    calendar.addPropertyChangeListener("calendar",calendarChangeListener);

This will disable selection of any date before current date and after 05/05/2015

Note that this API is not documented in their javadoc. But still this is a public setter that works as expected.

EDIT since you want to know how JDateChooser can be used in similar context

    JDateChooser chooser = new JDateChooser();
    chooser.getJCalendar().setSelectableDateRange(new Date(),new SimpleDateFormat("MM-DD-YYYY").parse("05-05-2015"));
    chooser.getJCalendar().addPropertyChangeListener("calendar",...);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...