I have a JavaFX dashboard that hides and shows components based on complicated contexts, so ReactFX is a great utility for this.
I created some nested closures by looping through each node, creating an EventStream
off eachvisibleProperty()
, and then subscribing an operation to switch the managedProperty()
and call sizeToScene()
. Although my solution works it does not feel very clean. I feel like I should be using a flatmap or something. Is there a more purely reactive way to implement this?
gridPane.getChildren().stream().forEach(c -> {
EventStreams.changesOf(c.visibleProperty()).subscribe(b -> {
c.managedProperty().set(b.getNewValue());
primaryStage.sizeToScene();
});
});
I will assume that the child list of your
gridPane
is fixed, since in your code you just iterate through it once.First, why not bind
managedProperty
of each child to itsvisibleProperty
?To get notified when any child changes its visibility, you can construct and observe a single
EventStream
:Since we assume the child list is fixed, you can get away with something slightly simpler:
All that said, I would consider finding a solution that does not tamper with
managedProperty
.EDIT: For example, filter the list of children by their visible property:
You might need to store a reference to
visibleChildren
to prevent it from being garbage collected, due to the use of weak listeners in JavaFX.