azro
azro

Reputation: 54168

Listener on some elements

A pretty easy question : I have 3 spinners and a DatePicker in my Pane to have a complete yyyy/mm/dd hh:mm:ss date (if there is a better solution than these 4 elements tell me)

And as we can do find_pseudo.textProperty().addListener(...) on a TextField I want to have a listener on the FOUR element, if one of the 4 has got changes -> hop i can do what I need to (it's set a predicate on a sorted list)

Upvotes: 0

Views: 122

Answers (1)

James_D
James_D

Reputation: 209684

You can do

ObjectBinding<LocalDateTime> dateTime = Bindings.createObjectBinding(() -> 
    LocalDateTime.of(
        datePicker.getValue(), 
        LocalTime.of(hourSpinner.getValue(), minuteSpinner.getValue(), secondsSpinner.getValue())),
    datePicker.valueProperty(), hourSpinner.valueProperty(), minuteSpinner.valuePropertY(), secondsSpinner.valueProperty());

Then dateTime.getValue() will be updated when any of the fields are updated, and is also observable, i.e. you can do

dateTime.addListener((obs, oldDateTime, newDateTime) -> {
    // do something with newDateTime
});

or

filteredList.predicateProperty().bind(Bindings.createObjectBinding(() -> 
    myObject -> myObject.getSomeDateTimeValue().isAfter(dateTime.getValue()),
    dateTime));

Upvotes: 2

Related Questions