kingkong
kingkong

Reputation: 1547

JavaFx Finding source parent node with specific id?

Is there is the way to find parent Node ( high in hierarchy ) via method? Element id or class can be use.

Any alternative to something like this?

source.getParent().getParent().getParent().getParent().getParent().getParent(); 

Upvotes: 9

Views: 20351

Answers (2)

Aaron
Aaron

Reputation: 5980

You can lookup any Node by its ID from the Scene object.

For example:

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

The ID is a CSS selector (id), or an FX ID. It has to be set up on the node without the '#' character. When invoking the method 'lookup', the '#' character has to precede the ID, like above.

Upvotes: 14

Marc
Marc

Reputation: 2639

Well I know you wanted to avoid that but still it is not that bad and does the work :

 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();

Upvotes: 3

Related Questions