Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

java - Selecting multiple items from combobox

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...