How to display geometry for few seconds?

I have attached a few geometries to a node that I wish to display on the GUI. I have gotten it to display, except when I try to make it disappear after a few seconds I run into issues.

[java]
app.getGuiNode().addLight(sun);
app.getGuiNode().attachChild(promptNode);

    StopWatch displayTime = new StopWatch();
    displayTime.start(); //Start the prompt timer.
    while (displayTime.getElapsedTimeSecs() < displayTimeSec) {}  //Default 2 seconds
    displayTime.stop();  //Stop the clock.
    app.getGuiNode().detachAllChildren();   //Clear prompt from display

[/java]

The objects will display if I only use the first two lines. The StopWatch class just keeps track of time, and I know that works fine because the program does pause for the set time. If I removed the last line, the objects would display after the rest of the code is executed. This is not what I want. I want the objects to appear, have the stopwatch count until the time is up, then the objects to disappear.

Is there a better/easier way for doing this?

Thanks

Are you doing that in another thread? else your whole app will just freeze in that empty while loop for 2 seconds.

Easiest way is to use the update loop. Something like:

[java]
private float counter = 0;
private float isShown = true;

public void update (float tpf) {

counter += tpf;

if (counter >= 2 && isShown) {
app.getGuiNode().detachAllChildren();
isShown = false;
}
}[/java]

Bonus points if you use an appstate/control and remove it when its not needed anymore.