Selecting multiple items from combobox

不打扰是莪最后的温柔 提交于 2019-11-28 05:57:00

问题


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.


回答1:


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

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.




回答2:


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);


来源:https://stackoverflow.com/questions/26186572/selecting-multiple-items-from-combobox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!