本文介绍了CheckComboBox(ControlsFX)设置为只读[JavaFX]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试找出如何设置 CheckComboBox
只读.
I have been trying to figure out how to set CheckComboBox
to read-only.
我不想禁用CheckComboBox
,因为我希望用户能够滚动并浏览已选中的项目,但是我想禁止选中/取消选中项目的功能.
I do not want to disable the CheckComboBox
because I want the user to be able to scroll and look through the already checked items, however I want to disallow the ability of checking/unchecking an item.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
虽然笨拙但可以使用:
public class CheckComboReadOnlySkin<T> extends CheckComboBoxSkin<T> {
public CheckComboReadOnlySkin(CheckComboBox control) {
super(control);
((ComboBox) getChildren().get(0)).setCellFactory((Callback<ListView<T>, ListCell<T>>) listView -> {
CheckBoxListCell<T> result = new CheckBoxListCell<>(item -> control.getItemBooleanProperty(item));
result.getStyleClass().add("readonly-checkbox-list-cell");
result.setDisable(true);
result.converterProperty().bind(control.converterProperty());
return result;
});
}
}
同时
checkComboBox.setSkin(new CheckComboReadOnlySkin<String>(checkComboBox));
完整用法:
final ObservableList<String> strings = FXCollections.observableArrayList();
for (int i = 0; i <= 50; i++)
strings.add("Item " + i);
// Create the CheckComboBox with the data
final CheckComboBox<String> checkComboBox = new CheckComboBox<>(strings);
for (int i = 0; i< checkComboBox.getCheckModel().getItemCount(); i++) {
if (i % 3 == 0)
checkComboBox.getCheckModel().check(i);
}
checkComboBox.setSkin(new CheckComboReadOnlySkin<String>(checkComboBox));
checkComboBox.getStylesheets().add(getClass().getResource("app.css").toString());
在app.css中:
.readonly-checkbox-list-cell{-fx-opacity : 1;}
.readonly-checkbox-list-cell .check-box{-fx-opacity : 1;}
结果:
我希望有人会提出更好的建议.
I hope someone will come up with a better one.
这篇关于CheckComboBox(ControlsFX)设置为只读[JavaFX]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!