JavaFX-如何在ListView中禁用CheckboxListCell?
我不知道如何在checklistview中禁用复选框。JavaFX-如何在ListView中禁用CheckboxListCell?,listview,javafx,javafx-8,Listview,Javafx,Javafx 8,我不知道如何在checklistview中禁用复选框。 CheckListView来自控件sfx。我是新手 任务对象,必须将其包装到复选框列表单元格中 public class Task { private final ReadOnlyStringWrapper name; private final ReadOnlyIntegerWrapper blockId; private final SimpleBooleanProperty isSelected; p
CheckListView
来自控件sfx。我是新手
任务对象,必须将其包装到复选框列表单元格中
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;
}
列表视图
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;
}
);
List tasksOfSection=domainModel.getTasks();
最终ObservableList tasks=FXCollections.observableArrayList(tasksOfSection);
最终CheckListView CheckListView=新的CheckListView(任务);
回调wrappedCellFactory=checkListView.getCellFactory();
checkListView.setCellFactory(listView->{
CheckBoxListCell=新的CheckBoxListCell();
如果(wrappedCellFactory!=null){
cell=(CheckBoxListCell)wrappedCellFactory.call(listView);
}
cell.setSelectedStateCallback((任务项)->item.selectedProperty());
//这将引发空指针异常。
cell.setDisable(!cell.getItem().isFound());
cell.itemProperty().addListener(新的ChangeListener()){
@凌驾
公共无效已更改(可观察值
- 首先,您必须将单元格结构重写为单个语句,以便它在函数中有效地成为最终语句
- 然后必须检查单元格的项是否为空(因为CheckListView也会创建空单元格来填充列表)
- 此外,还必须将NullpointerCheck和setDisable Methodcall包装到Platform.runLater中,因为它必须在JavaFX线程上执行(并且getItem Methodcall在cellfactory中始终返回null)
所以你的手机工厂应该是这样的:
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;
});
checkListView.setCellFactory(listView->{
CheckBoxListCell cell=wrappedCellFactory!=null?(CheckBoxListCell)wrappedCellFactory.call(listView):新建CheckBoxListCell();
cell.setSelectedStateCallback((任务项)->item.selectedProperty());
Platform.runLater(()->{
if(cell.getItem()!=null)
cell.setDisable(!cell.getItem().isFound());
});
cell.itemProperty().addListener(新的ChangeListener()){
@凌驾
公共无效已更改(ObservalEvalue谢谢:)
几个小时前,我终于解决了这个问题:
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;
});
checkListView.setCellFactory((ListView项)->{
final CheckBoxListCell cell=new CheckBoxListCell((任务t)->t.selectedProperty());
cell.itemProperty().addListener((obs、s、s1)->{
cell.disableProperty().unbind();
如果(s1!=null){
Task tm=tasks.stream().filter(t->t.getIdentifier()==s1.getIdentifier()).findFirst().orElse(null);
如果(tm!=null)
cell.disableProperty().bind(tm.disabledProperty());
}
});
返回单元;
});
但你的解决方案看起来更清晰