JavaFX CheckComboBox – Genuine Coder

JavaFX CheckComboBox

JavaFX Checkcombobox controlsfx

JavaFX CheckComboBox is a special UI control provided by ControlsFX. It is used for giving option to users to select more than one item from a Combobox. It is an extremely useful component you can use to provide a rich user experience. In this tutorial, we will explore the component usage with an example and style it using CSS. The basic idea of this component is Checkbox + ComboBox so that ComboBox can have multiple parallel selections.

Adding ControlsFX to Project

Since the component is from ControlsFX library, you need to include the dependency on your project. If you are using the maven, add the dependency as follows.

<dependency>
  <groupId>org.controlsfx</groupId>
  <artifactId>controlsfx</artifactId>
  <version>11.1.1</version>
</dependency>

If you are using Gradle, add the following line to your build.gradle file.

implementation 'org.controlsfx:controlsfx:11.1.1'

How to use CheckComboBox

The usage of CheckComboBox is similar to the normal JavaFX ComboBox. You can create a list of item you want to add and then attach it to the CheckComboBox.
In the following example, we will make use of CheckComboBox to keep things simlpe. Let’s see the usage on a sample code.

//Reduced import code
import org.controlsfx.control.CheckComboBox;

public class JavaFXCheckComboBoxTutorial extends Application {

  private CheckComboBox<String> createCheckComboBox() {
    // Create the list of items to be shown on the combobox
    ObservableList<String> programmingLanguages = FXCollections.observableArrayList(
        "Java",
        "Cpp",
        "Python",
        "C#",
        "Kotlin"
    );
    // Attach the list to the Combobox
    CheckComboBox<String> checkComboBox = new CheckComboBox<>(programmingLanguages);
    //As soon as an item is selected or selection is changed, display all the selected items
    checkComboBox.getCheckModel().getCheckedItems().addListener(new InvalidationListener() {
      @Override
      public void invalidated(Observable observable) {
        System.out.printf("\nSelected items: %s", getSelectedItems(checkComboBox));
      }
    });
    return checkComboBox;
  }

  /**
   * Get the currently selected items from the CheckComboBox
   */
  private List<String> getSelectedItems(CheckComboBox<String> checkComboBox) {
    return checkComboBox.getCheckModel().getCheckedItems();
  }

  @Override
  public void start(Stage stage) {
    VBox contentPane = new VBox(10);
    contentPane.setAlignment(Pos.CENTER);
    contentPane.getChildren().addAll(new Label("CheckComboBox Example"), createCheckComboBox());

    Scene scene = new Scene(contentPane, 400, 500);
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch();
  }
}

To get the list of selected items from the CheckComboBox, you can use

checkComboBox.getCheckModel().getCheckedItems()

It will return an ObservableList with all the selected items.

Customize CheckComboBox with CSS

Now, let’s see how to style the CheckComboBox with JavafX CSS styling. This component can be styled using combo-box-base CSS. By applying the following CSS, the CheckComboBox is customized with a lighter background.

.combo-box-base {
  -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, white;
  -fx-background-insets: 0 0 -1 0, 0, 1, 2;
  -fx-background-radius: 3px, 3px, 2px, 1px;
}

.combo-box .combo-box-popup .list-cell:hover {
  -fx-text-fill: yellow ;
  -fx-background-color: green ;
}

.combo-box .combo-box-popup .list-view, .combo-box .combo-box-popup .list-cell {
  -fx-background-color: black ;
  -fx-text-fill: white ;
}

Conclusion

In this tutorial, we have discussed how to make use of the CheckComboBox in code and style it using CSS. ControlsFX has a lot of such useful extra components. You might also be interested to checkout the following articles.

Comments

48 responses to “JavaFX CheckComboBox”

  1. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  2. … [Trackback]

    […] There you will find 89840 more Information to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  3. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  4. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  5. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  6. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  7. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  8. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  9. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  10. … [Trackback]

    […] Here you can find 27176 additional Info on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  11. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  12. … [Trackback]

    […] There you will find 69664 additional Information to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  13. … [Trackback]

    […] Information to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  14. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  15. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  16. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  17. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  18. … [Trackback]

    […] Here you will find 32268 more Info to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  19. … [Trackback]

    […] Here you will find 45651 additional Information on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  20. … [Trackback]

    […] Information on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  21. … [Trackback]

    […] Here you will find 27577 more Info on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  22. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  23. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  24. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  25. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  26. … [Trackback]

    […] There you will find 77118 more Info on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  27. … [Trackback]

    […] Find More Info here on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  28. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  29. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  30. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  31. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  32. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  33. … [Trackback]

    […] Here you will find 84228 additional Info on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  34. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  35. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  36. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/javafx-checkcombobox-with-example/ […]

  37. kamagra magasins de prix

    medicament kamagra pharmacie au rabais nouveau brunswick

  38. order enclomiphene generic pharmacy in canada

    enclomiphene non perscription

  39. get androxal cheap buy online no prescription

    cheapest buy androxal usa overnight delivery

  40. dutasteride shipped overnight no prescription

    how to buy dutasteride purchase online canada

  41. discount flexeril cyclobenzaprine ireland over the counter

    get flexeril cyclobenzaprine new zealand buy online

  42. how to buy gabapentin generic how effective

    buy cheap gabapentin purchase online from india

  43. discount fildena uk no prescription

    discount fildena uk online

  44. buy generic itraconazole from canada

    how to order itraconazole purchase in australia

  45. cheapest buy staxyn cheap online pharmacy

    cheap staxyn price south africa

  46. cheap avodart uk buy over counter

    how to order avodart purchase australia

  47. buying xifaxan generic ireland

    online order xifaxan online mastercard accepted

  48. ordering rifaximin canada online order

    online order rifaximin generic ingredients

Leave a Reply Cancel reply

Exit mobile version