Editing through a ChoiceBox/ComboBox

ChoiceBox provides a selection of preset values the user can choose from as a new value for the edited cell.

To use a ChoiceBox, we need to have an additional list of the potential new values:

// chaptep10/list/ChoiceBoxListCellDemo.javaObservableList<String> values = FXCollections.observableArrayList(        "Red", "Blue", "Yellow", "Green"); ObservableList<String> items = FXCollections.observableArrayList(        "Gray", "Gray", "Gray", "Gray"); ListView<String> list = new ListView<>(items);list.setEditable(true);list.setCellFactory(ChoiceBoxListCell.forListView(values));

Note that existing items in the ListView are not required to be chosen from values:

ComboBoxListCell works the same way, providing ComboBox instead ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.