从组合框中选择多个项目

问题描述 投票:0回答:4

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

java javafx fxml
4个回答
16
投票

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

checkcombobox

刚刚从 CheckComboBox javadoc 复制:

一个简单的 UI 控件,可以在类似 ComboBox 的控件中选择零个或多个项目。每行项显示一个 CheckBox,可以通过检查模型查询每行的状态。

 // 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().getCheckedItems().addListener(new ListChangeListener<String>() {
     public void onChanged(ListChangeListener.Change<? extends String> c) {
          while(c.next()) {
              //do something with changes here
          }
          System.out.println(checkComboBox.getCheckModel().getCheckedItems());
     }
 });
 }

注意:JavaFX 控件开发人员领导评论关于 JavaFX 内置组合框控件:

您可以将任何您想要的选择模型实例放入 ComboBox 中,但仅支持单个选择。我们这样做是因为如果不对 UI 和 UX 进行重大更改,多重选择并没有真正意义,并且我们认为将来可以开发一个单独的控件以更好地支持此用例

ControlsFX 中的 CheckComboBox 控件是单独的控件。


10
投票

我需要类似的东西,这解决了我的问题。

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

1
投票

我知道这是一篇旧文章,但这只是@user82426评论中描述的一个极简工作解决方案,并建议“加入”部分。如前所述,这是使用 http://javawiki.sowas.com/doku.php?id=javafx:combobox-multi-selection 构建的。

如上所述,它不是 COMBOBOX,而是 MENUBUTTON...尽管如此,它确实比 COMBOBOX 更好地满足了我的需求,所以我认为它可以帮助其他人;-)...

这里是:

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
投票

如果你看一下 ComboBox 类的源代码,它说“ComboBox 仅支持单选”。所以你需要制作一个自己的组合框,或者使用库,我推荐controlsFX。

© www.soinside.com 2019 - 2024. All rights reserved.