Selecting multiple items from combobox

后端 未结 3 1276
梦毁少年i
梦毁少年i 2020-12-20 00:32

Pls i want to know how to change the selectionmodel of javafxml combobox so that it can allow multiple seletion. Any contribution will be appreciated thanks.

相关标签:
3条回答
  • 2020-12-20 01:00

    I know this is an old post, but here is just a minimalist working solution as described by @user82426 comment with the 'joining' part suggested. This was built using, as mentioned, http://javawiki.sowas.com/doku.php?id=javafx:combobox-multi-selection.

    As stated it's not a COMBOBOX, but a MENUBUTTON... Nevertheless it does fill the needs I was looking for better than a COMBOBOX, so I thought it could help other ;-)...

    Here it is :

    import javafx.application.Application;
    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;
    import java.util.Arrays;
    import java.util.List;
    
    public class MultiSelectionComboDemo extends Application {
        final ListView<String> selectedItems = new ListView<>();
        
        @Override
        public void start(Stage primaryStage) {
            final String sMenuTextStart = "Fruit : ";
            final String sMenuTextEmpty = "[empty]";
            final MenuButton            choices = new MenuButton(sMenuTextStart+sMenuTextEmpty);
            final List<CheckMenuItem>   items   = Arrays.asList(new CheckMenuItem("Apple"), new CheckMenuItem("Banana"), new CheckMenuItem("Pear"), new CheckMenuItem("Kiwi"));
            choices.getItems().addAll(items);
            
            for (final CheckMenuItem item : items) {
                item.selectedProperty().addListener((observableValue, oldValue, newValue) -> {
                    if (newValue) {
                        selectedItems.getItems().add(item.getText());
                    } else {
                        selectedItems.getItems().remove(item.getText());
                    }
                    String sMenuText = sMenuTextStart + (selectedItems.getItems().size()>0?"":sMenuTextEmpty);
                    choices.setText(sMenuText+String.join(", ", selectedItems.getItems()));
                });
            }
            
            BorderPane borderPane = new BorderPane();
            borderPane.setTop(choices);
            borderPane.setCenter(selectedItems);
            primaryStage.setScene(new Scene(borderPane, 400, 300));
            primaryStage.show();
        }
        
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    0 讨论(0)
  • 2020-12-20 01:10

    I need something similar and this solved my problem.

    @FXML
    public MenuButton menuButton;  
    ......  
    CheckBox cb0 = new CheckBox("x");  
    CustomMenuItem item0 = new CustomMenuItem(cb0);  
    CheckBox cb1 = new CheckBox("y");  
    CustomMenuItem item1 = new CustomMenuItem(cb1);  
    item0.setHideOnClick(false);  
    item1.setHideOnClick(false);  
    menuButton.getItems().setAll(item0,item1);
    
    0 讨论(0)
  • 2020-12-20 01:23

    You could try an ControlsFX CheckComboBox (ControlsFX is a 3rd party controls library for JavaFX).

    checkcombobox

    Just copied from the CheckComboBox javadoc:

    A simple UI control that makes it possible to select zero or more items within a ComboBox-like control. Each row item shows a CheckBox, and the state of each row can be queried via the check model.

     // create the data to show in the CheckComboBox 
     final ObservableList<String> strings = FXCollections.observableArrayList();
     for (int i = 0; i <= 100; i++) {
         strings.add("Item " + i);
     }
    
     // Create the CheckComboBox with the data 
     final CheckComboBox<String> checkComboBox = new CheckComboBox<String>(strings);
    
     // and listen to the relevant events (e.g. when the selected indices or 
     // selected items change).
     checkComboBox.getCheckModel().getSelectedItems().addListener(new ListChangeListener<String>() {
         public void onChanged(ListChangeListener.Change<? extends String> c) {
             System.out.println(checkComboBox.getCheckModel().getSelectedItems());
         }
     });
     }
    

    Note: the JavaFX controls developer lead comments on the in-built combobox control for JavaFX:

    you can put whatever selection model instance you want into ComboBox, but only single selection will ever be supported. We did this as multiple selection didn't really make sense without drastic changes to the UI and UX, and we figured a separate control could be developed in the future to better support this use case

    The CheckComboBox control from ControlsFX is that seperate control.

    0 讨论(0)
提交回复
热议问题