我不知道如何在checklistview中创建禁用复选框。
CheckListView
来自 controlsfx 。我是新手。
任务对象,必须包装到复选框列表单元格中。
public class Task {
private final ReadOnlyStringWrapper name;
private final ReadOnlyIntegerWrapper blockId;
private final SimpleBooleanProperty isSelected;
private final boolean isFound;
public Task(String name, boolean isSelected, int blockId) {
this.name = new ReadOnlyStringWrapper(name);
this.isSelected = new SimpleBooleanProperty(isSelected);
this.blockId = new ReadOnlyIntegerWrapper(blockId);
this.isFound = isSelected;
}
public boolean isSelected() {
return isSelected.get();
}
public void setSelected(boolean selected) {
this.isSelected.set(selected);
}
public SimpleBooleanProperty selectedProperty() {
return isSelected;
}
public String getName() {
return name.get();
}
public ReadOnlyStringProperty nameProperty() {
return name.getReadOnlyProperty();
}
public int getBlockId() {
return blockId.get();
}
public ReadOnlyIntegerProperty blockIdProperty() {
return blockId.getReadOnlyProperty();
}
@Override
public String toString() {
return getName();
}
public boolean isFound() {
return isFound;
}
的ListView
List<Task> tasksOfSection = domainModel.getTasks();
final ObservableList<Task> tasks = FXCollections.observableArrayList(tasksOfSection);
final CheckListView<Task> checkListView = new CheckListView<>(tasks);
Callback<ListView<Task>, ListCell<Task>> wrappedCellFactory = checkListView.getCellFactory();
checkListView.setCellFactory(listView -> {
CheckBoxListCell<Task> cell = new CheckBoxListCell<>();
if (wrappedCellFactory != null) {
cell = (CheckBoxListCell<Task>) wrappedCellFactory.call(listView);
}
cell.setSelectedStateCallback((Task item) -> item.selectedProperty());
// This throw null pointer exception.
cell.setDisable(!cell.getItem().isFound());
cell.itemProperty().addListener(new ChangeListener<Task>(){
@Override
public void changed(ObservableValue<? extends Task> observable, Task oldValue, Task newValue) {
// I cannot modify here the checkbox cell...
}
});
return cell;
}
);
所以我在cell.setDisable()
上调用了CheckboxListCell
方法,但是我遇到了问题,当我用{{getItem()
调用它时,单元格的项目(任务对象)总是为空1}}方法。
是的,我试图覆盖updateItem
函数(),但是当我在列表视图中滚动时,复选框被设置为禁用,updateItem
正在监听每个事件,我认为这。所以这没用。
我想禁用isFound's
值为false的复选框,因为我想阻止用户点击它。但是用户应该看到完整列表。
您如何看待,我该怎么办?
感谢您的帮助。
答案 0 :(得分:2)
所以你的CellFactory应该是这样的:
checkListView.setCellFactory(listView -> {
CheckBoxListCell<Task> cell = wrappedCellFactory != null ? (CheckBoxListCell<Task>) wrappedCellFactory.call(listView) : new CheckBoxListCell<>();
cell.setSelectedStateCallback((Task item) -> item.selectedProperty());
Platform.runLater(() -> {
if (cell.getItem() != null)
cell.setDisable(!cell.getItem().isFound());
});
cell.itemProperty().addListener(new ChangeListener<Task>() {
@Override
public void changed(ObservableValue<? extends Task> observable, Task oldValue, Task newValue) {
// I cannot modify here the checkbox cell...
}
});
return cell;
});
答案 1 :(得分:0)
谢谢:)
几个小时前,我终于解决了这个问题:
checkListView.setCellFactory((ListView<Task> item) -> {
final CheckBoxListCell<Task> cell = new CheckBoxListCell<>((Task t) -> t.selectedProperty());
cell.itemProperty().addListener((obs, s, s1) -> {
cell.disableProperty().unbind();
if (s1!=null) {
Task tm = tasks.stream().filter(t -> t.getIdentifier() == s1.getIdentifier()).findFirst().orElse(null);
if (tm != null)
cell.disableProperty().bind(tm.disabledProperty());
}
});
return cell;
});
但您的解决方案看起来更清晰。