Dries Coppens
Dries Coppens

Reputation: 1085

JavaFx Button enabler in listview

I have 4 simple buttons that interact with a listview. If you click a certain button you can go one down, one up, to the start and to the end of the listview. So each button has a different onAction method: (Buttons: previous, next, end, start)

public void toNext(){
    list.getSelectionModel().selectNext();
}

public void toPrevious(){
    list.getSelectionModel().selectPrevious();
...

But now I want to disable the buttons if they can't go to the start, end, up or down. I tried to do the following by adding this code to the method toPrevious (example):

previous.setDisable(list.getSelectionModel().getSelectedIndex() == 0)

This code does disable the button but it won't enable it when you can go to the previous string in the listview. Does anyone has a simple solution for this?

Upvotes: 1

Views: 370

Answers (1)

James_D
James_D

Reputation: 209340

You can use a binding:

previous.disableProperty().bind(
    list.getSelectionModel().selectedIndexProperty().isEqualTo(0));

next.disableProperty().bind(
    list.getSelectionModel().selectedIndexProperty().isEqualTo(
        Bindings.size(list.getItems()).subtract(1)));

Upvotes: 2

Related Questions