Advertisement
sonrad10

JavaFX/ControlsFX SearchableCombobox Example

Sep 19th, 2020
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. import com.sun.javafx.scene.control.skin.ComboBoxListViewSkin;
  2. import impl.org.controlsfx.skin.SearchableComboBoxSkin;
  3. import javafx.application.Application;
  4. import javafx.collections.transformation.FilteredList;
  5. import javafx.scene.Node;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.ComboBox;
  8. import javafx.scene.control.Label;
  9. import javafx.scene.control.ListView;
  10. import javafx.scene.layout.BorderPane;
  11. import javafx.stage.Stage;
  12. import javafx.util.StringConverter;
  13. import org.controlsfx.control.SearchableComboBox;
  14.  
  15. public class Main extends Application {
  16.     public static void main(String[] args) {
  17.         launch(args);
  18.     }
  19.  
  20.     @Override
  21.     public void start(Stage primaryStage) {
  22.         //Setup
  23.         BorderPane root = new BorderPane();
  24.         primaryStage.setScene(new Scene(root));
  25.  
  26.         //Do some other stuff
  27.         root.setTop(new Label("SearchableComboBox Example:"));
  28.  
  29.         //...
  30.  
  31.         //Make the combobox
  32.         SearchableComboBox<Person> comboBox = makeCombobox();
  33.         root.setCenter(comboBox);
  34.  
  35.         //Optional way to override displaying the `toString()` values
  36.         //You can remove this if you want
  37.         comboBox.setConverter(new StringConverter<Person>() {
  38.             @Override
  39.             public String toString(Person obj) {
  40.                 return obj.fName + " " + obj.lName;
  41.             }
  42.  
  43.             @Override
  44.             public Person fromString(String string) {
  45.                 return null;
  46.             }
  47.         });
  48.  
  49.         //Do some more stuff
  50.         //Display the currently selected item at the bottom
  51.         Label lbl_selected = new Label();
  52.         comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
  53.             if (newValue == null) lbl_selected.setText("");
  54.             else lbl_selected.setText(newValue.toString());
  55.         });
  56.         root.setBottom(lbl_selected);
  57.  
  58.         //...
  59.  
  60.         //Set an initial selected value (optional)
  61.         comboBox.getSelectionModel().select(10);
  62.  
  63.         //Finally show the window
  64.         primaryStage.show();
  65.     }
  66.  
  67.     /**
  68.      * @return  A searchable combobox with some default values
  69.      */
  70.     private SearchableComboBox<Person> makeCombobox() {
  71.         //Some values to display in the combobox
  72.         Person[] people = new Person[] {
  73.                 new Person("Chandler", "Bing"),
  74.                 new Person("Ross", "Geller"),
  75.                 new Person("Monica", "Geller"),
  76.                 new Person("Rachel", "Green"),
  77.                 new Person("Phoebe", "Buffay"),
  78.  
  79.                 new Person("April", "Ludgate"),
  80.                 new Person("Leslie", "Knope"),
  81.                 new Person("Ron", "Swanson"),
  82.                 new Person("Jerry", "Gergich"),
  83.                 new Person("Andy", "Dwyre"),
  84.                 new Person("Ann", "Perkins"),
  85.                 new Person("Donna", "Meagle"),
  86.                 new Person("Tom", "Haverford"),
  87.                 new Person("Mark", "Brendanawicz")
  88.         };
  89.  
  90.         //Make the combobox
  91.         SearchableComboBox<Person> comboBox = new SearchableComboBox<>();
  92.         comboBox.getItems().addAll(people);
  93.  
  94.         //================================
  95.         //If you want to programmatically select a value you should include the following as well
  96.         //Chances are you will, so this will save you a bit of debugging/searching
  97.         //See the bug report here: https://bugs.openjdk.java.net/browse/JDK-8129123
  98.  
  99.         //Automatically scroll to the last selected value when opening
  100.         comboBox.showingProperty().addListener((observable, oldValue, newValue) -> {
  101.             int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
  102.             //Do nothing on close or if nothing is selected
  103.             if (!newValue || selectedIndex < 0) return;
  104.  
  105.             //Get the inner combobox used to display the elements in the popup
  106.             SearchableComboBoxSkin<?> searchableComboBoxSkin = (SearchableComboBoxSkin<?>) comboBox.getSkin();
  107.             FilteredList<Node> filtered = searchableComboBoxSkin.getChildren().filtered(node -> node instanceof ComboBox<?>);
  108.             if (filtered.size() <= 0) return;
  109.             ComboBox<?> innerComboBox = (ComboBox<?>) filtered.get(0);
  110.  
  111.             //Scroll to the selected element in the combobox
  112.             ComboBoxListViewSkin<?> skin = (ComboBoxListViewSkin<?>) innerComboBox.getSkin();
  113.             skin.getListView().scrollTo(selectedIndex);
  114.             ((ListView) skin.getPopupContent()).scrollTo(selectedIndex);
  115.         });
  116.         //================================
  117.  
  118.         //Return the created combobox
  119.         return comboBox;
  120.     }
  121. }
  122.  
  123. /**
  124.  * An example class to be used in the combobox.
  125.  */
  126. class Person {
  127.     public String fName = "";
  128.     public String lName = "";
  129.  
  130.     public Person(String fName, String lName) {
  131.         this.fName = fName;
  132.         this.lName = lName;
  133.     }
  134.  
  135.     public String toString() {
  136.         return lName + ", " + fName;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement