Logical scene graph traversal API

Michael Strauß michaelstrau2 at gmail.com
Thu Aug 6 16:16:28 UTC 2020

Every now and then I have encountered the need to traverse and inspect the
JavaFX scene graph.
In one recent example, my task was to selectively adorn UI controls with
other controls for enhanced accessibility.
Other people have also tried to do similar things, as indicated by some
StackOverflow questions and blog posts [0].

The JavaFX scene graph consists of user-created nodes, as well as nodes
that are created by control skins.
While APIs like `Parent.getChildrenUnmodifiable` can be used to traverse
the scene graph, this only works once it is connected to a scene and skins
have been applied.

The scene graph can also change quite dramatically depending on particular
control skins, and generally does not correspond to the "logical graph"
that was defined in FXML or in code.
Adding an API like `Parent.getLogicalChildrenUnmodifiable` would solve that
issue quite easily, and it would also enable the inspection of subgraphs
that are not connected to a scene.
The logical graph could be used to inspect the document structure of the
code, independent of the applied control skins.

For example, `Button.getLogicalChildrenUnmodifiable` would return a single
node, namely the value of the ´graphic` property (or no node, if there is
no graphic).
`ScrollPane.getLogicalChildrenUnmodifiable` would return the value of the
`content` property, and so on.

The downside is, of course, that the responsibility of defining logical
nodes would need to be imposed on third-party controls.
It stands to reason that such a feature might need to be an opt-in feature
in order to prevent breaking changes.
However, even if only implemented in the core controls that ship with
JavaFX, it might still be a useful addition for many applications.

[0] http://www.kware.net/?p=8

More information about the openjfx-dev mailing list