Search code examples
eventsjavafxjfxtras

jfxtras - CalendarPicker getDisplayedCalendar in EventFilter wrong


I have a CalendarPicker with an EventFilter. In the EventFilter I want the displayedCalendar with CalendarPicker.getDisplayedCalendar(), but I get the wrong date. It is the last date before CalendarPicker update the displayedCalendar.
It seems that my EventFilter is executed before the standard events of the CalendarPicker.
And I cannot use an EventHandler because the event is consumed by CalendarPicker.
For example:
- I see March 2015 in CalendarPicker.
- change the month to April 2015
- in my CalendarPiker.addEventFilter() I get with CalendarPicker.getDisplayedCalendar() "2015-03-01"
- the CalendarPicker shows April 2015

How can I execute the standard Events before my EventFilter?

Here is some code:

calendarpicker.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
   if (event.getButton().equals(MouseButton.PRIMARY)) {
      Calendar cal = calendarpicker.getCalendar();
      if (cal != null) {
        selectedDate = cal.getTime();
        log.debug(String.format("(selectedDate): %s" , selectedDate));
        log.debug(String.format("(displayed): %s" , calendarpicker.getDisplayedCalendar().getTime()));
      }
    }
  }
});

If I pick a date in the calendarpicker, everything is o.k.
The selectedDate is the picked date and displayed calendar is the 1th day of the displayed month.
If I use the spinner to change the month or the year, the selectedDate do not change (I think this is correct because I do not select one date), but the displayed date is the 1th date of the month before I changed the month (e.g. change from april to march => 01.04.2015 instead of 01.03.2015).


Solution

  • I understand what you want. However, events are fired in some order, and there is no rule mandating what order that should be. In this case there is nothing mandating that the mouse clicked event must be fired after any properties have changed.

    Are you not listening for the wrong event? If you would register a listener on the displayedCalendarProperty() you would be informed when the displayed date changes, no matter what triggered it (a mouse click, keyboard navigation -ListSpinner supports that-, or programatically).