JavaFx Looking for source node parent with specific id?

Is there a way to find the parent Node (high level hierarchy) using the method? The id element or class can be used.

Any alternative to something like this?

source.getParent().getParent().getParent().getParent().getParent().getParent(); 
+6
source share
2 answers

You can find any Node by its identifier from the Scene object.

For instance:

 Scene scene = source.getScene(); Node nodeToFind = scene.lookup("#nodeToFindId"); 

An identifier is a CSS selector (id) or an identifier of an FX. It must be configured on Node without the '#' character. When the "lookup" method is called, the "#" character must precede the identifier, as described above.

+9
source

Well, I know that you wanted to avoid this, but still this is not so bad and does the job:

  Node node = youNode; while (node != null){ node = node.getParent(); } Node parentNode = node; 

Otherwise, if you have access to the scene object:

  Node parentNode = scene.getRoot(); 
0
source

Source: https://habr.com/ru/post/952127/


All Articles