0

how can I listen to a change in the CalendarTextField in JFXtras? For example a new choosen date from the picker-menu or a typed-in date?

    date.addEventFilter(MouseEvent.ANY, new EventHandler<Event>() {

        @Override
        public void handle(Event arg0) {
            System.out.println("EVENT");

        }
    });

gives me every movement of the mouse within the field. I didn't find another eventType that makes sense.

I also thought about adding this event filter in the window and check the selected date at every click in the window. But that can't be the right way.

Steven
  • 441
  • 4
  • 16

2 Answers2

2

Alright, found it here:

    date.valueProperty().addListener(new ChangeListener<Calendar>() {
        @Override
        public void changed(
                ObservableValue<? extends Calendar> observableValue,
                Calendar oldValue, Calendar newValue) {
            System.out.println(oldValue + " -> " + newValue);
        }
    });

I didn't realize that in FX a listener has to be set to the property, not to the component like in Swing.

Steven
  • 441
  • 4
  • 16
0

I have redesigned a calendar component in a control with FXML, for the picker-menu I use the XCalendarPicker and I add a changelistener to it calendar() (who is a property). And when a date is changed I update my textfield with the new Date

final XCalendarPicker calendarPicker = new XCalendarPicker();

final ChangeListener<Calendar> calendarListener = new ChangeListener<Calendar>() {

            @Override
            public void changed(ObservableValue<? extends Calendar> observable, Calendar oldValue, Calendar newValue) {
                Date date = newValue.getTime();
                setDate(date);
            }
        };

        calendarPicker.calendar().addListener(calendarListener);
agonist_
  • 4,890
  • 6
  • 32
  • 55
  • Thanks! What was your reason for not using the CalendarTextField in JFXtras? I'm sure there's a way to add a listener to it. – Steven Jun 11 '13 at 09:23
  • The only reason, it I don't want user can type there own date, they can only choose from the picker. but I'm sure if I add a listener to my textfield it can be edited with no problems, you just need to check if the entered date match with your dateformat. – agonist_ Jun 11 '13 at 09:46