load Window Size from Preferences - Java java.awt

Java examples for java.awt:Window

Description

load Window Size from Preferences

Demo Code


//package com.java2s;

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;

import java.util.prefs.Preferences;

public class Main {
    public static void loadWindowSize(Window window, int defaultWidth,
            int defaultHeight) {
        final Preferences prefs = Preferences.userNodeForPackage(window
                .getClass());/*from ww w .ja  va  2s .  co m*/
        int x = prefs.getInt("x", 0);
        int y = prefs.getInt("y", 0);
        int w = prefs.getInt("w", defaultWidth);
        int h = prefs.getInt("h", defaultHeight);
        final Rectangle maxRectangle = GraphicsEnvironment
                .getLocalGraphicsEnvironment().getMaximumWindowBounds();
        if (w + x > maxRectangle.width) {
            x = 0;
            w = maxRectangle.width;
        }
        if (h + y > maxRectangle.height) {
            y = 0;
            h = maxRectangle.height;
        }
        if (x < 0) {
            x = 0;
        }
        if (System.getProperty("os.name").indexOf("Mac") >= 0 && y < 20) {
            y = 20;
        }
        window.setBounds(x, y, w, h);
    }
}

Related Tutorials