Items changed event for JavaFX ListView control


Question

I am looking for something like an Item Changed Event or Item Count Changed Event for JavaFX ListView control or lets just say in general for any collection type control.

It is because, I have some Buttons, that I want to be enabled only when there is at least one item in ListView otherwise that Button should be in disabled state.

It is my guess that perhaps adding a ChangeListener to the ListView control. would that be a right approach.

Any suggestions how can we achieve this.

1
0
9/16/2014 2:32:46 PM

The JavaFX Listview provides a method with the signature

public final ObservableList<T> getItems()

You can add a listener to the observable list which will be called whenever items are added to or removed from the ListView.

aListView.getItems().addListener(new ListChangeListener() {
    @Override
    public void onChanged(ListChangeListener.Change change) {
        System.out.println("Detected a change! ");
    }
});

Similar functionality is also provided by the other 'collection' controls.

2
9/16/2014 2:36:45 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon