8 Replies Latest reply: Dec 15, 2012 6:34 AM by MiPa RSS

    Store last window position and size

    943372
      Hi,

      is there a mechanism in JavaFX 2 to store/restore the last window position and size. With last I mean the position shall be saved if the application is closed and restored when the application is started. Also when more than one screen is available. Do I have to do it on my own or is there already something in FX 2?

      Regards
      Oliver
        • 1. Re: Store last window position and size
          shakir.gusaroff
          Hi. You can save the last window position and size:
          stage.setOnCloseRequest(
                          new EventHandler<WindowEvent>() {
                              public void handle(final WindowEvent event) {
                                  System.out.println(scene.getX());  //save 
                                  System.out.println(scene.getX());
                                  System.out.println(scene.getWidth());
                                  System.out.println(scene.getHeight());
                                  
                              }
                          });
          • 2. Re: Store last window position and size
            943372
            Hi,

            ok...so I can get settings to save. But the saving and restoring I have to do on my own?
            • 3. Re: Store last window position and size
              James_D
              Yes, you'll need to manage saving and retrieving these yourself. The Java Preferences API is probably the simplest way to do this: http://docs.oracle.com/javase/7/docs/api/java/util/prefs/package-summary.html
              • 4. Re: Store last window position and size
                James_D
                Here's an example:
                import java.util.prefs.Preferences;
                
                import javafx.application.Application;
                import javafx.scene.Scene;
                import javafx.scene.control.Label;
                import javafx.scene.layout.BorderPane;
                import javafx.scene.text.Font;
                import javafx.stage.Stage;
                
                public class MemoryWindow extends Application {
                
                  private Stage primaryStage;
                
                  @Override
                  public void start(Stage primaryStage) {
                    this.primaryStage = primaryStage;
                    Label label = new Label("Hello World");
                    label.setFont(new Font("Serif", 36));
                    BorderPane root = new BorderPane();
                    root.setCenter(label);
                
                    Preferences userPrefs = Preferences.userNodeForPackage(getClass());
                    // get window location from user preferences: use x=100, y=100, width=400, height=400 as default
                    double x = userPrefs.getDouble("stage.x", 100);
                    double y = userPrefs.getDouble("stage.y", 100);
                    double w = userPrefs.getDouble("stage.width", 400);
                    double h = userPrefs.getDouble("stage.height", 400);
                
                    Scene scene = new Scene(root);
                    primaryStage.setScene(scene);
                    primaryStage.setX(x);
                    primaryStage.setY(y);
                    primaryStage.setWidth(w);
                    primaryStage.setHeight(h);
                    primaryStage.show();
                  }
                
                  @Override
                  public void stop() {
                    Preferences userPrefs = Preferences.userNodeForPackage(getClass());
                    userPrefs.putDouble("stage.x", primaryStage.getX());
                    userPrefs.putDouble("stage.y", primaryStage.getY());
                    userPrefs.putDouble("stage.width", primaryStage.getWidth());
                    userPrefs.putDouble("stage.height", primaryStage.getHeight());
                  }
                
                  public static void main(String[] args) {
                    launch(args);
                  }
                }
                Also, for your question regarding the number of screens; as I understand it, the bounds for the stage (x, y, width, and height) are relative to a coordinate system that spans all screens. So this technique should let the window "remember" which screen it was on.

                If the user unplugged a screen between invocations of the application, I'm not sure what would happen. You could do something like
                if (Screen.getScreensForRectangle(x, y, width, height).size()==0) {
                  // previous bounds are not on any available screen
                  // revert to some default values
                }
                if you wanted to be sure nothing bad happened. It seems reasonable to assume the OS would cope with this scenario though.
                • 5. Re: Store last window position and size
                  jsmith
                  The preferences thing is interesting James, I hadn't seen that before, I always just used properties files.
                  • 6. Re: Store last window position and size
                    shakir.gusaroff
                    Javafx script had a nice feature: Javafx script Storage API:
                    http://docs.oracle.com/cd/E17802_01/javafx/javafx/1.2/docs/api/javafx.io/javafx.io.Storage.html

                    It was very useful for storing and retrieving data.
                    I am wondering why the Javafx team did not include the storage API in Javafx2.0.
                    • 7. Re: Store last window position and size
                      jsmith
                      Perhaps a Storage service would be redundant because Java already has a FileService.
                      http://docs.oracle.com/javase/7/docs/technotes/guides/javaws/developersguide/examples.html#FileSaveService
                      • 8. Re: Store last window position and size
                        MiPa
                        This brings up another interesting question. Does JavaFX support the JNLP services? Some of them seem to be very closely tied in with Swing and I am not sure whether they would work in a pure JavaFX context. Also the JavaFX deployment guide does not mention them with a single word. (At least not when I last had a look at it.)
                        Michael