Javafx: ListView disable a specified element of th

2019-07-13 20:00发布

问题:

I have a ListView<String> and I want to disable one specified element of the list, to become unselectable for the user. Is there a way to disable just one specified element for selection?

回答1:

Thanks to @user1803551 and @James_D i could solve the problem, here is the sollution:

listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
        @Override 
        public ListCell<String> call(ListView<String> param) {
            return new ListCell<String>() {
                @Override 
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if ("Orange".equals(item)) {
                        setDisable(true);
                    } else {
                        setDisable(false);
                    }
                    setText(item);
                }

            };
        }
    });