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.
Doing it your way, this would work:
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.
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:
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.
Use a
PauseTransition
: