How to delete item from a ComboBox after selecting it?

1

I use a ComboBox to add an object to a TableView and wanted it to be right after the user clicked on the desired object, this item was added in TableView and exited the ComboBox list.

I tried this way but gave NullPointerException :

public void handleAddTeacherEvaluator(){

    Teacher t = addTeachersBox.getSelectionModel().getSelectedItem();
    auxTeachers.add(t); //adicionando na ObservableList da tableView
    addTeachersBox.getItems().remove(t);
}
    
asked by anonymous 19.05.2017 / 22:37

1 answer

0

Try to adapt the following solution to your context: (As I can not simulate your context I 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 contextMenu closes and there is something selected (index> 0). It is important that this if exists to avoid errors does not remove. Hope it helps!

    
22.06.2017 / 17:30