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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

1 Answers1

6

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",...);
ring bearer
  • 20,383
  • 7
  • 59
  • 72
  • 1
    Min, max and range methods are specified in the `IDateEditor` interface. – trashgod Apr 05 '12 at 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? – Jayyrus Apr 05 '12 at 12:17
  • @user1190704 - Hm! That is tricky. Please consult code in edited answer. – ring bearer Apr 05 '12 at 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? – Jayyrus Apr 05 '12 at 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 Apr 05 '12 at 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 choosen – Jayyrus Apr 05 '12 at 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 Apr 05 '12 at 14:51
  • ok, i used datechooser. You're right, is better. But .getDateEditor().setMinSelectableDate won't work :( – Jayyrus Apr 05 '12 at 15:45
  • No no no.. you have to to this - `chooser.getJCalendar().setMinSelectableDate(..)` – ring bearer Apr 05 '12 at 16:03