2

Is there a simple way to retain the position of a JavaFX Stage that has been hidden? In Swing, when a Frame was hidden and made visible again, it would retain the position and dimensions it had when it was hidden. Is there a simple way this behavior can be achieved in JavaFX?

In my application, I have a main window with toggle buttons which are meant to be tied to showing / hiding secondary windows. Unfortunately, every time a user hides one of those secondary windows (using the toggle buttons), they open up at the center of the screen again if the user tries to make them visible again.

Any simple way around this?

public static void main(final String[] args) { Platform.startup(() -> {}); Platform.runLater(() -> testStagePos()); } protected static void testStagePos() { final BorderPane p = new BorderPane(); p.setCenter(new Label("Random label")); p.setPadding(new Insets(100)); // Just so window isn't too small final Stage popup = new Stage(); popup.setScene(new Scene(p)); final BorderPane cp = new BorderPane(); final ToggleButton b = new ToggleButton("Show popup"); b.setOnAction(e -> { if (b.isSelected()) popup.show(); else popup.hide(); }); cp.setPadding(new Insets(50)); cp.setCenter(b); final Stage control = new Stage(); control.setScene(new Scene(cp)); control.show(); } 
4
  • Before you hide the Stage save the Stage's position. After you show it again reset the Stage's position. Commented Feb 16, 2018 at 1:58
  • I may not be relevant to the question but where is Platform.startup(() defined ? I can't find it in the doc. Commented Feb 16, 2018 at 7:22
  • Surprising.. I expect the Stage to keep the current position. As an alternative you can use iconification. setIconified saves the current position. Commented Feb 16, 2018 at 8:07
  • @KDM Thanks for the iconification suggestion! Unfortunately my application can open up to 15 different popups and the iconified windows still show up in the task bar. Understandably, it's an annoyance for users. Commented Feb 16, 2018 at 17:42

2 Answers 2

5

Add this just after creating the popup.

 popup.setOnShown((e) -> { popup.setX(popup.getX()); popup.setY(popup.getY()); }); 

Building up on https://stackoverflow.com/a/48822302/600379

Sign up to request clarification or add additional context in comments.

3 Comments

Note that this is alternative to popup.setX(50);popup.setY(50); . No need for both. +1
Thanks!! This did the trick! I'm a little confused about why it works though. I expected that the position information would have been lost at the closing. But the fact that this works seems to suggest that it isn't really lost just reset AFTER being shown...
@anishthecoder had a look at the Stage code. Stage is keeping two variables implicitX and implicitY and once set - it uses those. If not set it calculates each time on show.
3

If you set any initial position to popup, it will retain its last position when hidden.
All you have to change is add this after defining popup:
popup.setX(50);popup.setY(50);

Test it by:

private static void testStagePos() { final BorderPane p = new BorderPane(); p.setCenter(new Label("Random label")); p.setPadding(new Insets(100)); // Just so window isn't too small final Stage popup = new Stage(); popup.setScene(new Scene(p)); popup.setX(50);popup.setY(50); //set initial position final BorderPane cp = new BorderPane(); final ToggleButton b = new ToggleButton("Show popup"); b.setOnAction(e -> { if (b.isSelected()) { popup.show(); b.setText("Hide popup"); } else { System.out.println(popup == null ); popup.hide(); b.setText("Show popup"); } }); cp.setPadding(new Insets(50)); cp.setCenter(b); final Stage control = new Stage(); control.setScene(new Scene(cp)); control.show(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.