How to close a stage after a certain amount of time JavaFX
Asked Answered
P

3

10

I'm currently working with two controller classes.

In Controller1 it creates a new stage that opens on top of the main one.

Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Controller2.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

Now once that stage is open, I want it to stay open for about 5 seconds before closing itself.

Within Controller2, I've tried implementing something like

long mTime = System.currentTimeMillis();
long end = mTime + 5000; // 5 seconds 

while (System.currentTimeMillis() > end) 
{
      //close this stage  
} 

but I have no idea what to put inside the while loop to close it. I've tried all sorts and nothing works.

Prescription answered 6/12, 2014 at 17:35 Comment(0)
P
30

Use a PauseTransition:

PauseTransition delay = new PauseTransition(Duration.seconds(5));
delay.setOnFinished( event -> stage.close() );
delay.play();
Phinney answered 6/12, 2014 at 17:54 Comment(0)
P
0

This code sets the text of a TextArea element and makes it visible for a certain amount of time. It essentially creates a pop up system message:

public static TextArea message_text=new TextArea();

final static String message_text_style="-fx-border-width: 5px;-fx-border-radius: 10px;-fx-border-style: solid;-fx-border-color: #ff7f7f;";

public static int timer;
public static void system_message(String what,int set_timer)
{

    timer=set_timer;

    message_text.setText(what);
    message_text.setStyle("-fx-opacity: 1;"+message_text_style);

    Thread system_message_thread=new Thread(new Runnable()
    {

        public void run()
        {

            try
            {
                Thread.sleep(timer);
            }
            catch(InterruptedException ex)
            {

            }

            Platform.runLater(new Runnable()
            {

                public void run()
                {

                    message_text.setStyle("-fx-opacity: 0;"+message_text_style);

                }   

            });

        }   

    });

    system_message_thread.start();

}

This solution is completely general. You can change the setStyle methods to any code that you want. You can open and close a stage if you like.

Pippa answered 6/6, 2015 at 21:15 Comment(0)
M
-1

Doing it your way, this would work:

long mTime = System.currentTimeMillis();
long end = mTime + 5000; // 5 seconds 

while (mTime < end) 
{
    mTime = System.currentTimeMilis();
} 
stage.close();

You need to save your stage into a variable. Maybe it is better to run that in a Thread, so that you can do something within the 5 seconds. Another way would be to run a Thread.sleep(5000); and this would also be more performant than the while loop.

Marivaux answered 6/12, 2014 at 17:45 Comment(1)
If you use these kind of techniques, you would have to run that in a thread, else the contents of the stage would not display. Moreover, you would have to wrap stage.close() in a Platform.runLater(...) as that has to be executed on the FX Application Thread. It's easier to use a PauseTransition.Phinney

© 2022 - 2024 — McMap. All rights reserved.