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

Go To StackoverFlow.com

1

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?

2012-04-05 01:36
by JackTurky


7

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",...);
2012-04-05 05:16
by ring bearer
Min, max and range methods are specified in the IDateEditor interface - trashgod 2012-04-05 10:44
there are two libraries jcalendar. In one,method setSelectableDataRange is defined but is not defined the addDateListener. In the other is defined addDateListener but not setSelectableDataRange.. how can i solve it? i need both them method. otherwise if i use the library in which is defined setSelectableDateRange, how can i get date clicked from user in order to add that date to a jtextfield - JackTurky 2012-04-05 12:17
@user1190704 - Hm! That is tricky. Please consult code in edited answer - ring bearer 2012-04-05 12:58
ok, it selects date, but being Jcalendar added to JPopupMenu, in the propertyChangeListener, i have to hide popup and when i call mypopup.setVisible(false), it launch stackoverflowError, how can i solve it - JackTurky 2012-04-05 13:57
What are you trying to do? - you may want to explain. Why don't you use JDateChooser and its getJCalendar() instead if you are looking for a popup selector - ring bearer 2012-04-05 14:32
i'm trying to: Create JPopupMenu, on click on JTextField add JCalendar to JPopupMenu and show it. When user choose date on jcalendar, hide JPopupMenu and set the text of JTextField with the date choose - JackTurky 2012-04-05 14:36
Dear @user1190704 - Whatever you are trying can be neatly achieved by JDateChooser - Anyway, adding popup and setting text value to selected date does not cause SO exception. It may be due to something else - ring bearer 2012-04-05 14:51
ok, i used datechooser. You're right, is better. But .getDateEditor().setMinSelectableDate won't work : - JackTurky 2012-04-05 15:45
No no no.. you have to to this - chooser.getJCalendar().setMinSelectableDate(..)ring bearer 2012-04-05 16:03
Ads