有一个界面:
public interface DecisionWrapper {
boolean decision ();
BooleanProperty decisionProperty ();
void setDecision (boolean decision);
String getName ();
}
还有一个TreeView <DecisionWrapper>
,显示CheckBoxTreeItem <DecisionWrapper>
中的相应元素。点击CheckBox
,decisionProperty
必须更改值。
我知道我可以为每个TreeItem
添加监听器,并更改decisionProperty
。但我认为这是解决问题的一个不好的方法。我试图在cellFactory
构造函数中将CheckBoxTreeCell
作为decisionProperty
提供selectionProperty
,但显然它不起作用:
treeViewReport.setCellFactory (CheckBoxTreeCell.forTreeView (param -> param.getValue().decisionProperty()));
// Here, in fact, the same.
treeViewReport.setCellFactory (new Callback <TreeView <DecisionWrapper>, TreeCell <DecisionWrapper >> () {
@Override
public TreeCell <DecisionWrapper> call (TreeView <DecisionWrapper> param) {
return new CheckBoxTreeCell <DecisionWrapper> (param1 -> param1.getValue().decisionProperty ());
}
});
定义TreeView
:
treeViewReport.setShowRoot(false);
treeViewReport.setCellFactory(CheckBoxTreeCell.forTreeView(param -> param.getValue().decisionProperty()));
CheckBoxTreeItem<DecisionWrapper> root = new CheckBoxTreeItem<>(new DefaultDecisionWrapper("", DecisionWrapper.WrapperType.NEW));
CheckBoxTreeItem<DecisionWrapper> newVls;
CheckBoxTreeItem<DecisionWrapper> oldVls;
if (!importResult.getNewVls().isEmpty()) {
if (importResult.isOnlyMessages()) {
newVls = new CheckBoxTreeItem<>(new DefaultDecisionWrapper("New Messages", DecisionWrapper.WrapperType.NEW));
List<CheckBoxTreeItem<DecisionWrapper>> messages = new ArrayList<>();
importResult.getNewVls().get(0).getMessageWrappers().forEach(msgDecision -> messages.add(new CheckBoxTreeItem<>(msgDecision)));
newVls.getChildren().addAll(messages);
} else {
newVls = new CheckBoxTreeItem<>(new DefaultDecisionWrapper("New Vls", DecisionWrapper.WrapperType.NEW));
for (DecisionVlWrapper vlDecision : importResult.getNewVls()) {
CheckBoxTreeItem<DecisionWrapper> vl = new CheckBoxTreeItem<>(vlDecision);
List<CheckBoxTreeItem<DecisionWrapper>> messages = vlDecision.getMessageWrappers()
.stream()
.map((Function<DecisionWrapper, CheckBoxTreeItem<DecisionWrapper>>) CheckBoxTreeItem::new)
.collect(Collectors.toList());
vl.getChildren().addAll(messages);
newVls.getChildren().add(vl);
}
}
root.getChildren().add(newVls);
}
Class DefaultDecisionWrapper:
public class DefaultDecisionWrapper implements DecisionWrapper {
BooleanProperty decision;
WrapperType wrapperType;
String name;
public DefaultDecisionWrapper(String name,WrapperType wrapperType){
decision = new SimpleBooleanProperty(false);
this.wrapperType = wrapperType;
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public boolean decision() {
return decision.get();
}
@Override
public BooleanProperty decisionProperty() {
return decision;
}
@Override
public void setDecision(boolean decision) {
this.decision.set(decision);
}
@Override
public WrapperType getWrapperType() {
return wrapperType;
}
}
提示如何最好地绑定值?
UPD:我找到了临时解决方案。添加了新课程:
public class CheckBoxTreeItemBind<T extends DecisionWrapper> extends CheckBoxTreeItem<T> {
public CheckBoxTreeItemBind(T t){
super(t);
selectedProperty().bindBidirectional(getValue().decisionProperty());
}
}
但我认为,它也不好。