Can someone show me how to disable some item of my combobox (With FXML or Java code)? here is my ComboBox:
<ComboBox fx:id="cBox">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Easy" />
<String fx:value="Normal" />
<String fx:value="Hard" />
</FXCollections>
</items>
</ComboBox>
Thanks!
i didn't found any methods that can inactive ComboBox items. You can try this work around , below code is to display sublist of items dynamically(use this idea to solve your problem).
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));
} ...
I was trying to achieve this and I came up with a custom ComboBox that disables the items I don't want the user to select. Below code shows the custom ComboBox class and how to use it.
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]);
}
}
}
Then add the items to disable to the ComboBox:
@FXML private CustomComboBox<String> customComboBox;
...
customComboBox.setDisabledItems("Item 2", "Item 3");
And change the class in the fxml file:
<views.custom.CustomComboBox ... />
comboBox.setItems(FXCollections.observableArrayList(EnumValues.values()));
comboTipoOperacoes.getItems().remove(4); // remove the item 4 of Enums.
I had the same issue and I think that the best solution to this problem is to use the
setCellFactory(Callback,ListCell> value) method of 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
}
}
};
}
});
and if you want a custon ButtonCel you need to use the setButtonCell(ListCell value) Method:
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
}
}
});