java 从组合框中选择多个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26186572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting multiple items from combobox
提问by Odum
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.
请我想知道如何更改 javafxml 组合框的选择模型,以便它可以允许多个选择。任何贡献将不胜感激,谢谢。
回答by jewelsea
You could try an ControlsFX CheckComboBox(ControlsFXis a 3rd party controls library for JavaFX).
您可以尝试使用ControlsFX CheckComboBox(ControlsFX是 JavaFX 的第 3 方控件库)。
Just copied from the CheckComboBox javadoc:
刚刚从 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()); } }); }
一个简单的 UI 控件,可以在类似 ComboBox 的控件中选择零个或多个项目。每个行项显示一个 CheckBox,可以通过 check 模型查询每行的状态。
// 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 commentson the in-built combobox control for JavaFX:
注意:JavaFX 控件开发人员对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
您可以将任何您想要的选择模型实例放入 ComboBox,但只支持单个选择。我们这样做是因为如果没有对 UI 和 UX 进行大幅度更改,多重选择就没有真正意义,我们认为将来可以开发一个单独的控件来更好地支持这个用例
The CheckComboBox control from ControlsFX is that seperate control.
ControlsFX 的 CheckComboBox 控件是单独的控件。
回答by erolmuhammet.cs
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);