Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 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 Answer

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",...);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...