
// list that fires updates if any members change visibility:ObservableList<Node> children = FXCollections.observableArrayList(n -> new Observable[] {n.visibleProperty()});// make the new list always contain the same elements as the pane's child list:Bindings.bindContent(children, parentPane.getChildren());// filter for visible nodes:ObservableList<Node> visibleChildren = children.filter(Node::isVisible);// and now see if it's empty:BooleanBinding someVisibleChildren = Bindings.isNotEmpty(visibleChildren);// finally:parentPane.visibleProperty().bind(someVisibleChildren);另一种方法是
BooleanBinding直接创建自己的:
Pane parentPane = ... ;BooleanBinding someVisibleChildren = new BooleanBinding() { { parentPane.getChildren().forEach(n -> bind(n.visibleProperty())); parentPane.getChildren().addListener((Change<? extends Node> c) -> { while (c.next()) { c.getAddedSubList().forEach(n -> bind(n.visibleProperty())); c.getRemoved().forEach(n -> unbind(n.visibleProperty())) ; } }); bind(parentPane.getChildren()); } @Override public boolean computevalue() { return parentPane.getChildren().stream() .filter(Node::isVisible) .findAny() .isPresent(); }}parentPane.visibleProperty().bind(someVisibleChildren);欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)