Java 从组合框中选择多个项目

Java 从组合框中选择多个项目,java,javafx,fxml,Java,Javafx,Fxml,请告诉我如何更改javafxml combobox的selectionmodel,使其可以进行多次选择。任何贡献都将不胜感激。您可以尝试(是JavaFX的第三方控件库) 刚刚从复选框javadoc复制: 一个简单的UI控件,可以在类似组合框的控件中选择零个或多个项。每行项目显示一个复选框,可以通过检查模型查询每行的状态 // create the data to show in the CheckComboBox final ObservableList<String> st

请告诉我如何更改javafxml combobox的selectionmodel,使其可以进行多次选择。任何贡献都将不胜感激。

您可以尝试(是JavaFX的第三方控件库)

刚刚从复选框javadoc复制:

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

 // 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());
     }
 });
 }
//创建要在复选框中显示的数据
最终ObservableList字符串=FXCollections.observableArrayList();

对于(inti=0;i我需要类似的东西,这就解决了我的问题

@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注释所述,建议使用“加入”部分

如前所述,它不是一个组合框,而是一个菜单按钮……尽管如此,它确实比组合框更好地满足了我的需求,所以我认为它可以帮助其他人;-)

这是:

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);
    }
}
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.CheckMenuItem;
导入javafx.scene.control.ListView;
导入javafx.scene.control.MenuButton;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
导入java.util.array;
导入java.util.List;
公共类MultiSelectionComboDemo扩展了应用程序{
最终ListView selectedItems=新建ListView();
@凌驾
公共无效开始(阶段primaryStage){
最后一个字符串sMenuTextStart=“Fruit:”;
最后一个字符串sMenuTextEmpty=“[empty]”;
最终菜单按钮选项=新建菜单按钮(sMenuTextStart+SMENUTEXTPEMPTY);
最终列表项=Arrays.asList(新CheckMenuItem(“苹果”)、新CheckMenuItem(“香蕉”)、新CheckMenuItem(“梨”)、新CheckMenuItem(“猕猴桃”);
choices.getItems().addAll(items);
用于(最终检查菜单项:项){
item.selectedProperty().addListener((ObservalEvalue、oldValue、newValue)->{
如果(新值){
选择editems.getItems().add(item.getText());
}否则{
选择editems.getItems().remove(item.getText());
}
字符串sMenuText=sMenuTextStart+(selectedItems.getItems().size()>0?“:SMENUTEXTMPTY);
choices.setText(sMenuText+String.join(“,”,selectedItems.getItems());
});
}
BorderPane BorderPane=新的BorderPane();
边框窗格.setTop(选项);
borderPane.setCenter(selectedItems);
设置场景(新场景(边框窗格,400300));
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

组合框和菜单按钮是不同的控件。OP想要一个组合框。是的,但无论如何这是一个建设性的答案。我更喜欢这个解决方案,也可以用一个{CheckMenuItem}替换{CheckBox,CustomMenuItem}的组合。代码的一个示例位于。此外,还可以添加一个侦听器,根据所选项目的列表自动重命名菜单按钮。例如String.join()。我非常喜欢这个答案。我一整天都在试图将这个答案整合到桌面视图中,但收效甚微。这能很容易做到吗?完美的解决方案,但上帝说这件事很难看。有没有办法让复选框像组合框一样接受用户输入?这将是有益的,当有许多选择在checkcombobox@YHStan请把你的问题作为新问题提出来。