【问题标题】:How to disable some items of javaFX ComboBox?如何禁用 javaFX ComboBox 的某些项目?
【发布时间】:2012-11-15 05:53:11
【问题描述】:

谁能告诉我如何禁用我的组合框的某些项目(使用 FXML 或 Java 代码)?这是我的组合框:

<ComboBox fx:id="cBox">
  <items>
    <FXCollections fx:factory="observableArrayList">
      <String fx:value="Easy" />
      <String fx:value="Normal" />
      <String fx:value="Hard" />
    </FXCollections>
  </items>
</ComboBox>

谢谢!

【问题讨论】:

    标签: combobox javafx-2 javafx


    【解决方案1】:

    我没有找到任何可以使ComboBox 项目无效的方法。你可以试试这个解决方法,下面的代码是动态显示项目的子列表(使用这个想法来解决你的问题)。

    private final ObservableList<String> allOptions = 
                FXCollections.observableArrayList("Easy","Normal","Hard");
    
       // method which returns sublist we need
        private ObservableList<String> getSubList(int start,int end) {
    
        final ObservableList<String> toBeDisplayedList = FXCollections
                .<String> observableArrayList();
        toBeDisplayedList.addAll(allOptions.subList(start, end));
        return toBeDisplayedList;
        }
    
       // now main logic
     if(displayAll) {
              comboBox.setItems(allOptions);
             }
     if(display only easy and normal) {
          comboBox.setItems(getSublist(0,2));
      } ...
    

    【讨论】:

    【解决方案2】:

    我试图实现这一点,我想出了一个自定义组合框,它可以禁用我不希望用户选择的项目。下面的代码展示了自定义的 ComboBox 类以及如何使用它。

        public class CustomComboBox<T> extends ComboBox<T> {
    
        private ArrayList<T> disabledItems = new ArrayList<T>();
    
        public CustomComboBox() {
            super();
            setup();
        }
    
        public CustomComboBox(ObservableList<T> list) {
            super(list);
            setup();
        }
    
        private void setup() {
    
            SingleSelectionModel<T> model = new SingleSelectionModel<T>() {
    
                @Override
                public void select(T item) {
    
                    if (disabledItems.contains(item)) {
                        return;
                    }
    
                    super.select(item);
                }
    
                @Override
                public void select(int index) {
                    T item = getItems().get(index);
    
                    if (disabledItems.contains(item)) {
                        return;
                    }
    
                    super.select(index);
                }
    
                @Override
                protected int getItemCount() {
                    return getItems().size();
                }
    
                @Override
                protected T getModelItem(int index) {
                    return getItems().get(index);
                }
    
            };
    
            Callback<ListView<T>, ListCell<T>> callback = new Callback<ListView<T>, ListCell<T>>() {
    
                @Override
                public ListCell<T> call(ListView<T> param) {
                    final ListCell<T> cell = new ListCell<T>() {
                        @Override
                        public void updateItem(T item, boolean empty) {
    
                            super.updateItem(item, empty);
    
                            if (item != null) {
    
                                setText(item.toString());
    
                                if (disabledItems.contains(item)) {
                                    setTextFill(Color.LIGHTGRAY);
                                    setDisable(true);
                                }
    
                            } else {
    
                                setText(null);
    
                            }
                        }
                    };
    
                    return cell;
                }
    
            };
    
            setSelectionModel(model);
            setCellFactory(callback);
    
        }
    
        public void setDisabledItems(T... items) {
            for (int i = 0; i < items.length; i++) {
                disabledItems.add(items[i]);
            }
        }
    
    }
    

    然后将要禁用的项目添加到 ComboBox:

    @FXML private CustomComboBox<String> customComboBox;
    ...
    customComboBox.setDisabledItems("Item 2", "Item 3");
    

    并更改fxml文件中的类:

    <views.custom.CustomComboBox ... />
    

    【讨论】:

    • stackoverflow.com/questions/32370394/…中的另一种(可能更简单?)方法
    • 值得注意的是,与其从 java 设置颜色,不如从 CSS 设置它。如果您删除setTextFill 行并添加一个CSS 项目.list-cell:disabled { -fx-text-fill: #D3D3D3 /*or -fx-opacity: 0.40*/ },您将获得相同的效果,但是当您将CSS 应用到您的应用程序时,您不需要上吊。这是因为setDisable(true) 导致单元格被标记为 CSS 伪类“已禁用”。
    【解决方案3】:

    我遇到了同样的问题,我认为解决这个问题的最佳方法是使用 setCellFactory(Callback,ListCell> value)ComboBox 的方法:

            cBox.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> param)
            {
                return new ListCell<String>() {
                    @Override
                    protected void updateItem(String item, boolean empty)
                    {
                        super.updateItem(item, empty);
    
                        if (item != null || !empty)
                        {
                            this.setText(item);
                            this.setDisable(true); //or false
                        }
                    }
                };
            }
        });
    

    如果您想要自定义 ButtonCel,则需要使用 setButtonCell(ListCell value) 方法:

            cBox.setButtonCell(new ListCell<String>() {
            @Override
            protected void updateItem(Stringitem, boolean empty)
            {
                super.updateItem(item, empty);
    
                if (item != null || !empty)
                {
                    this.setText(item);
                    this.setDisable(true); //or false
                }
            }
        });
    

    【讨论】:

      【解决方案4】:
      comboBox.setItems(FXCollections.observableArrayList(EnumValues.values()));  
      comboTipoOperacoes.getItems().remove(4); // remove the item 4 of Enums.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-27
        • 1970-01-01
        • 2018-09-06
        • 2015-09-08
        • 1970-01-01
        • 2013-04-04
        • 2010-11-09
        相关资源
        最近更新 更多