5 Replies Latest reply on Oct 29, 2013 7:53 PM by user627817

    ComboBox with checkbox

    user627817

      Hi All,

       

      I want to display a combobox with ablity to select multiple items and also be able to show selected items with different visuals - I think a checkbox to show selected/un-selected item is more appropriate but I can live with the alternative, as for example, highlight the selected ones with "bright green" etc.

       

      Is it possible to do this under JavaFX?

       

      Thanks,

      DP

        • 1. Re: ComboBox with checkbox
          jsmith

          Your UI description does seem kind of strange to me.

           

          Usually a combo box is used to select a value from a drop down or manually enter a value.

          What will you render for the button cell? ComboBox (JavaFX 2.2) buttonCell

           

          I've never seen a multiple selection combobox before.

          I don't think you can easily create one as such a design is not fit within the original use-case intentions of the combo box design.

           

          ComboBox (JavaFX 2.2) selectionModelProperty states "The selection model for the ComboBox. A ComboBox only supports single selection."

           

          • 2. Re: ComboBox with checkbox
            James_D

            I agree with jsmith that this sounds like an "off-label" use of a combo box, and you'd need to work around the API quite a lot to make it work.

             

            Consider instead using a MenuButton populated with CheckMenuItems.

            • 3. Re: ComboBox with checkbox
              user627817

              You right - I am not equipped with even a modest java UI knowledge, and last touched Java in 1.4 version with some Swing UI development.

              Now I need to come up with this small app.

               

              I want to display list of items, allow at a time a single item is to be selected, but once selected, item remains selected - could be unselected if clicked on it again. So if a list have 10 items, I could select 1 item at a time, and click 3 different items, those 3 are now selected. Also once selected, I need to make it appear differently so user will know which are "selected", which are "not selected" and which are "disabled".

               

              If I click on one of those 3 items again, I will unselect them.

               

              It could be a ListView component.

               

              If I could make an item with a "checkbox - TextDescription", it would be ideal.

               

              Thanks!

              DP

              • 4. Re: ComboBox with checkbox
                James_D
                import java.util.Arrays;
                import java.util.List;
                
                import javafx.application.Application;
                import javafx.beans.value.ChangeListener;
                import javafx.beans.value.ObservableValue;
                import javafx.scene.Scene;
                import javafx.scene.control.CheckMenuItem;
                import javafx.scene.control.ListView;
                import javafx.scene.control.MenuButton;
                import javafx.scene.layout.BorderPane;
                import javafx.stage.Stage;
                
                public class MultipleSelectionDropdownTest extends Application {
                
                    @Override
                    public void start(Stage primaryStage) {
                        final MenuButton choices = new MenuButton("Fruit");
                        final List<CheckMenuItem> checkItems = Arrays.asList(
                            new CheckMenuItem("Apples"), 
                            new CheckMenuItem("Oranges"), 
                            new CheckMenuItem("Pears"), 
                            new CheckMenuItem("Grapes"), 
                            new CheckMenuItem("Mangoes")
                        );
                        choices.getItems().addAll(checkItems);
                
                          // Keep track of selected items
                        final ListView<String> selectedItems = new ListView<>();
                        for (final CheckMenuItem item : checkItems) {
                            item.selectedProperty().addListener(new ChangeListener<Boolean>() {
                                @Override
                                public void changed(ObservableValue<? extends Boolean> obs,
                                        Boolean wasPreviouslySelected, Boolean isNowSelected) {
                                    if (isNowSelected) {
                                        selectedItems.getItems().add(item.getText());
                                    } else {
                                        selectedItems.getItems().remove(item.getText());
                                    }
                                }
                            });
                        }
                
                        BorderPane root = new BorderPane();
                        root.setTop(choices);
                        root.setCenter(selectedItems);
                        primaryStage.setScene(new Scene(root, 600, 400));
                        primaryStage.show();
                    }
                
                    public static void main(String[] args) {
                        launch(args);
                    }
                }
                
                • 5. Re: ComboBox with checkbox
                  user627817

                  Works!

                   

                  Thanks so much!!

                   

                  -DP