Store last window position and size943372 Dec 13, 2012 8:04 AM
Hi,
This content has been marked as final. Show 8 replies
1. Re: Store last window position and sizeshakir.gusaroff Dec 13, 2012 9:20 AM (in response to 943372)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 size943372 Dec 13, 2012 9:37 AM (in response to shakir.gusaroff)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 sizeJames_D Dec 13, 2012 10:05 AM (in response to 943372)Yes, you'll need to manage saving and retrieving these yourself. The Java Preferences API is probably the simplest way to do this:
4. Re: Store last window position and sizeJames_D Dec 13, 2012 1:29 PM (in response to 943372)Here's an example:
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.
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); } }
If the user unplugged a screen between invocations of the application, I'm not sure what would happen. You could do something like
if you wanted to be sure nothing bad happened. It seems reasonable to assume the OS would cope with this scenario though.
if (Screen.getScreensForRectangle(x, y, width, height).size()==0) { // previous bounds are not on any available screen // revert to some default values }
5. Re: Store last window position and sizejsmith Dec 13, 2012 12:28 PM (in response to James_D)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 sizeshakir.gusaroff Dec 14, 2012 8:23 PM (in response to 943372)Javafx script had a nice feature: Javafx script Storage API:
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 sizejsmith Dec 15, 2012 5:25 AM (in response to shakir.gusaroff)Perhaps a Storage service would be redundant because Java already has a FileService.
8. Re: Store last window position and sizeMiPa Dec 15, 2012 6:34 AM (in response to jsmith)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