0

I am creating a javaFx application that runs in Mac OSX El Capitan. When I create a bundle from my jar file to .app file, the application window dimension changes.

I want window dimension to be the same before and after bundling. Any help is appreciated.

Pete
  • 57,112
  • 28
  • 117
  • 166
cystemd
  • 1
  • 2

1 Answers1

1

As I known JavaFX doesn't save/restore window position/dimension automatically.

So, you should restore window position/dimension before show window and save then before window hide. For example I am using such helper:

import javafx.stage.Stage;

import java.util.prefs.Preferences;

public class StagePositionManager {

    public static final String IS_FIRST_RUN_KEY = "isFirstRun";
    public static final String X_KEY = "x";
    public static final String Y_KEY = "y";
    public static final String WIDTH_KEY = "width";
    public static final String HEIGHT_KEY = "height";

    private final Stage stage;
    private final Class<?> windowClass;

    public StagePositionManager(Stage stage, Class<?> windowClass) {
        this.stage = stage;
        this.windowClass = windowClass;

        restoreStagePosition();
        stage.setOnHidden(event -> saveStagePosition());
    }

    private void saveStagePosition() {
        Preferences preferences = Preferences.userNodeForPackage(windowClass);

        preferences.putBoolean(IS_FIRST_RUN_KEY, false);

        preferences.putDouble(X_KEY, stage.getX());
        preferences.putDouble(Y_KEY, stage.getY());
        preferences.putDouble(WIDTH_KEY, stage.getWidth());
        preferences.putDouble(HEIGHT_KEY, stage.getHeight());
    }

    private void restoreStagePosition() {
        Preferences preferences = Preferences.userNodeForPackage(windowClass);

        if (!preferences.getBoolean(IS_FIRST_RUN_KEY, true)) {
            stage.setX(preferences.getDouble(X_KEY, 0));
            stage.setY(preferences.getDouble(Y_KEY, 0));
            stage.setWidth(preferences.getDouble(WIDTH_KEY, 1024));
            stage.setHeight(preferences.getDouble(HEIGHT_KEY, 768));
        }
    }

}

and call it after application start:

public class MyApplication extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        ...
        new StagePositionManager(primaryStage, Main.class);
        ...
Maxim
  • 9,701
  • 5
  • 60
  • 108