How to delete item from a Combobox after selecting it?

Asked

Viewed 423 times

1

I use a ComboBox to add an object to a TableView and wanted that soon after the user click on the desired object, this item was added in the TableView and left the list of ComboBox.

I tried it like this NullPointerException:

public void handleAddTeacherEvaluator(){

    Teacher t = addTeachersBox.getSelectionModel().getSelectedItem();
    auxTeachers.add(t); //adicionando na ObservableList da tableView
    addTeachersBox.getItems().remove(t);
}

1 answer

0


Try to adapt solution following to your context: (As I have no way to simulate your context tested only with index removal)

combobox.setOnHidden((event) -> {
        int index = combobox.getSelectionModel().getSelectedIndex();
        if(index > 0){ // Evita erros quando não há seleção
            combobox.getSelectionModel().clearSelection(); 
            combobox.getItems().remove(index);
        }
    });

When an item is selected the normal behavior of the combobox is to close the menu, so I put something to run every time the context menu closes and there is something selected (index > 0). It is important that this if exists to prevent errors in remove. I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.