An simple autofill approach to extend the default JavaFX's combobox behavior.
The default combobox component of JavaFX dont offer a option to query his list of items. This code is a powerful solution to this problem.
- You has an ordered list of strings
-
For type safe reasons we use .toLowerCase() function at input string and on search. If your list contains only upper/lower case strings you can remove the .toLowerCase() from search.
items.stream().filter(p -> p.toLowerCase().startsWith(lower)).findFirst();
With a predefined list:
ObservableList<String> items = FXCollections.observableArrayList();
// Add items to list
AutoFillComboBox autoFill = new AutoFillComboBox(items);
Without a predefined list of items:
AutoFillComboBox autoFill = new AutoFillComboBox();
// some code
ObservableList<String> items = FXCollections.observableArrayList();
autoFill.setItems(items);
autoFill.setAutoFill(true);