This discussion is archived
6 Replies Latest reply: Feb 14, 2013 2:24 AM by Gen.Java RSS

lookup problems

Gen.Java Newbie
Currently Being Moderated
Hi all,

Please check the following code:
package helloworld;

import javafx.application.Application;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;

public class HelloWorld extends Application
{

public static void main(String[] args)
{
launch(args);
}

public void start(Stage primaryStage)
{
Accordion accordion = new Accordion();

TitledPane t1 = new TitledPane("T1", new Button("B1"));
t1.setId ( "t1" );

accordion.getPanes().addAll(t1);
accordion.setId ( "a.b" );

Group root = new Group();
root.getChildren().add(accordion);

System.out.println ( root.lookup("#a.b") );
System.out.println ( root.lookup("#t1") );

primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}

}
The previous code has two problems:

1. The first lookup() prints null because of the dot (.). How can I lookup nodes whose ids have dots? I tried "#a\00002Eb", still no luck
2. The second lookup() prints null ? no idea why

Thank you
  • 1. Re: lookup problems
    shakir.gusaroff Expert
    Currently Being Moderated
    public Node lookup(java.lang.String selector)
         
     
    I think the node id should not contain a period because in css files class selectors are preceded by a period (.).



    >The second lookup() prints null ? no idea why
    If you call lookup() after primaryStage.show() you can see the correct id.
            primaryStage.show();
            System.out.println(root.lookup("#t1")); 
      
  • 2. Re: lookup problems
    Gen.Java Newbie
    Currently Being Moderated
    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() ?
  • 3. Re: lookup problems
    shakir.gusaroff Expert
    Currently Being Moderated
    I think it is a bug. There is a similar one:
    http://javafx-jira.kenai.com/browse/RT-18104

    Jira says:
    Node.lookup() method does not expose children of TabPane(tabs) and SplitPane(items) . It only works when the scene is realized (shown). Resolution: Not an issue.
    I think it is still an issue.
  • 4. Re: lookup problems
    Gen.Java Newbie
    Currently Being Moderated
    shakir.gusaroff wrote:
    I think it is still an issue.
    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.
  • 5. Re: lookup problems
    jsmith Guru
    Currently Being Moderated
    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:
    parent.getChildren().add(new Button());
    Node n = parent.lookup(".button");
    That it is pretty counter-intuitive that the lookup would return null at that time.

    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.
  • 6. Re: lookup problems
    Gen.Java Newbie
    Currently Being Moderated
    Thank you jsmith for your explanation and advice.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points