繁体   English   中英

从组合框中选择多个项目

[英]Selecting multiple items from combobox

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

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

复选框

刚刚从 CheckComboBox javadoc 复制:

一个简单的 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 (eg 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 控件开发人员JavaFX的内置组合框控件的评论

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

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

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

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

我知道这是一篇旧帖子,但这里只是一个极简主义的工作解决方案,如@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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM