java - 从组合框中选择多个项目

标签 java javafx fxml

我想知道如何更改java fxml组合框的选择模型,以便它可以允许多重选择。任何贡献将不胜感激。

最佳答案

您可以尝试 ControlsFX CheckComboBox ( ControlsFX 是 JavaFX 的第 3 方控件库)。

checkcombobox

刚刚从 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());
     }
 });
 }

注意:JavaFX controls developer lead comments在 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

ControlsFX 中的 CheckComboBox 控件就是那个单独的控件。

关于java - 从组合框中选择多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26186572/

相关文章:

java - 获取 HTTPS 连接的响应码

java - 单例类对象存在多久

JavaFX:获取所选行单元格的文本

java - 在JavaFX的FXML中,如何声明自定义事件的处理程序?

java - 从 Controller 类编辑在 fxml 中创建的 UI 元素 - JavaFX

Javafx设置背景

java - JTextArea 到文件

java - 将 SparQL 查询转换为 QueryBuilder 对象,以便我可以使用准备好的语句

JavaFX/TreeTableView : Relationship between Selection and Focus

java - 如何将动态 JVM 命令行标志传递给独立的 JavaFX 应用程序?