Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.javafx.scene.control.skin.ComboBoxListViewSkin;
- import impl.org.controlsfx.skin.SearchableComboBoxSkin;
- import javafx.application.Application;
- import javafx.collections.transformation.FilteredList;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.ComboBox;
- import javafx.scene.control.Label;
- import javafx.scene.control.ListView;
- import javafx.scene.layout.BorderPane;
- import javafx.stage.Stage;
- import javafx.util.StringConverter;
- import org.controlsfx.control.SearchableComboBox;
- public class Main extends Application {
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) {
- //Setup
- BorderPane root = new BorderPane();
- primaryStage.setScene(new Scene(root));
- //Do some other stuff
- root.setTop(new Label("SearchableComboBox Example:"));
- //...
- //Make the combobox
- SearchableComboBox<Person> comboBox = makeCombobox();
- root.setCenter(comboBox);
- //Optional way to override displaying the `toString()` values
- //You can remove this if you want
- comboBox.setConverter(new StringConverter<Person>() {
- @Override
- public String toString(Person obj) {
- return obj.fName + " " + obj.lName;
- }
- @Override
- public Person fromString(String string) {
- return null;
- }
- });
- //Do some more stuff
- //Display the currently selected item at the bottom
- Label lbl_selected = new Label();
- comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
- if (newValue == null) lbl_selected.setText("");
- else lbl_selected.setText(newValue.toString());
- });
- root.setBottom(lbl_selected);
- //...
- //Set an initial selected value (optional)
- comboBox.getSelectionModel().select(10);
- //Finally show the window
- primaryStage.show();
- }
- /**
- * @return A searchable combobox with some default values
- */
- private SearchableComboBox<Person> makeCombobox() {
- //Some values to display in the combobox
- Person[] people = new Person[] {
- new Person("Chandler", "Bing"),
- new Person("Ross", "Geller"),
- new Person("Monica", "Geller"),
- new Person("Rachel", "Green"),
- new Person("Phoebe", "Buffay"),
- new Person("April", "Ludgate"),
- new Person("Leslie", "Knope"),
- new Person("Ron", "Swanson"),
- new Person("Jerry", "Gergich"),
- new Person("Andy", "Dwyre"),
- new Person("Ann", "Perkins"),
- new Person("Donna", "Meagle"),
- new Person("Tom", "Haverford"),
- new Person("Mark", "Brendanawicz")
- };
- //Make the combobox
- SearchableComboBox<Person> comboBox = new SearchableComboBox<>();
- comboBox.getItems().addAll(people);
- //================================
- //If you want to programmatically select a value you should include the following as well
- //Chances are you will, so this will save you a bit of debugging/searching
- //See the bug report here: https://bugs.openjdk.java.net/browse/JDK-8129123
- //Automatically scroll to the last selected value when opening
- comboBox.showingProperty().addListener((observable, oldValue, newValue) -> {
- int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
- //Do nothing on close or if nothing is selected
- if (!newValue || selectedIndex < 0) return;
- //Get the inner combobox used to display the elements in the popup
- SearchableComboBoxSkin<?> searchableComboBoxSkin = (SearchableComboBoxSkin<?>) comboBox.getSkin();
- FilteredList<Node> filtered = searchableComboBoxSkin.getChildren().filtered(node -> node instanceof ComboBox<?>);
- if (filtered.size() <= 0) return;
- ComboBox<?> innerComboBox = (ComboBox<?>) filtered.get(0);
- //Scroll to the selected element in the combobox
- ComboBoxListViewSkin<?> skin = (ComboBoxListViewSkin<?>) innerComboBox.getSkin();
- skin.getListView().scrollTo(selectedIndex);
- ((ListView) skin.getPopupContent()).scrollTo(selectedIndex);
- });
- //================================
- //Return the created combobox
- return comboBox;
- }
- }
- /**
- * An example class to be used in the combobox.
- */
- class Person {
- public String fName = "";
- public String lName = "";
- public Person(String fName, String lName) {
- this.fName = fName;
- this.lName = lName;
- }
- public String toString() {
- return lName + ", " + fName;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement