This content has been marked as final. Show 6 replies
I think the node id should not contain a period because in css files class selectors are preceded by a period (.).
public Node lookup(java.lang.String selector)
>The second lookup() prints null ? no idea why
If you call lookup() after primaryStage.show() you can see the correct id.
Thank you shakir:
I think the node id should not contain a period because in css files class selectors are preceded by a period (.).Yes. I checked CSS ID Specification and found that a period (.) is not allowed.
If you call lookup() after primaryStage.show() you can see the correct id.Ok. Is there any way you can lookup before calling show() ?
shakir.gusaroff wrote:Thank you shakir, I hope they can resolve this issue soon because lookup() is a great method that has no equivalent in Swing. I agree with you that it is an issue because in some situations, you do need to lookup components before realizing the scene.
I think it is still an issue.
you do need to lookup components before realizing the scene.I am not sure that that is possible based on the current JavaFX architecture.
I also don't think that that is quite what the linked jira issue in the thread was referring to.
I do agree that if you execute code like the following:
That it is pretty counter-intuitive that the lookup would return null at that time.
parent.getChildren().add(new Button()); Node n = parent.lookup(".button");
It is, I think, notable that equivalent jQuery style lookups in the html allow you to lookup a change to the DOM after you modify it (perhaps html takes a huge efficiency penalty to allow such lookups to function).
Potential workarounds are:
1. Wrap your lookup calls in Platform.runLater
Often that will work for me as it gives the JavaFX system time to run a rendering pulse on the scene which gets the scenegraph all set up with appropriate nodes and css applied to it before you do your lookup.
2. Use an AnimationTimer and poll the scene with a lookup each time the timer is invoked, then stops polling once the lookup succeeds.
(I think this is a pretty rare case where this is needed). If the scenegraph changes take a couple of pulses to propagate (which for some weird reason some of the chart scenegraph changes seem to do - perhaps because charts can be animated), then this is like a more heavyweight solution to the Platform.runLater method, for which the lookups can work at a later time.
3. I think there are some private methods in the JavaFX API which I have never used which manually kick off a css processing pass (something like impl_processCss - though I haven't tried to find the actual name).
Perhaps if you manually trigger the css processing on the scene graph, then subsequent lookups will work without you having to resort to tricker like Platform.runLater.
I think overall, it is unclear whether using lookup functions extensively in JavaFX code is as good an idea as it is in jQuery code. My overall feeling is - no it is not and the lookup style should be avoided in favor of direct API calls or, if the purpose of the lookup is just related to applying style changes, applying style changes based on setting css styleclasses on components and making use of a css stylesheet.
I found that often I was able to replace dynamic code lookups with css styleclasses and stylesheets. So I'd advise you to look at the reason on why you are doing the lookup, and, if possible, just make use of the css stylesheet mechanism instead.