Berry Berry - 1 month ago 10
Java Question

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

I have a

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

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

How would I solve this?

Answer Source

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: