Berry
Berry

Reputation: 2298

How to detect whether the user has selected an item on a ListView?

I have a ListView with items in it, and to go to the next page, the user must select an item, because the next page is about modifying the selected item.

To go to the next page, the user clicks a button labeled "Next". I have this grayed out by default, but I'd like it to be un-grayed the moment the user clicks on an element in the ListView.

At the moment, I have it set on onMouseClick which isn't very effective because this activates quite literally whenever the ListView node is clicked, not when an element is.

How would I solve this?

Upvotes: 1

Views: 1434

Answers (1)

DVarga
DVarga

Reputation: 21799

You can check the original tutorial, section "Processing the List Item Selection" (Example 11-5).

listView.getSelectionModel().selectedItemProperty().addListener((obs, oldVal, newVal) -> {
    // newVal contains the selected item
});

Also you can bind disableProperty of the Button to the selectedItemProperty of the selection model of the ListView conditionally:

nextButton.disableProperty().bind(listView.getSelectionModel().selectedItemProperty().isNull());

Upvotes: 3

Related Questions