1

I'm writing a java program, I have used Andrew Thompson's code from here: Best practice for setting JFrame locations:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;
import java.io.*;

class RestoreMe {

    /** This will end up in the current directory
    A more sensible location is a sub-directory of user.home.
    (left as an exercise for the reader) */
    public static final String fileName = "options.prop";

    /** Store location & size of UI */
    public static void storeOptions(Frame f) throws Exception {
        File file = new File(fileName);
        Properties p = new Properties();
        // restore the frame from 'full screen' first!
        f.setExtendedState(Frame.NORMAL);
        Rectangle r = f.getBounds();
        int x = (int)r.getX();
        int y = (int)r.getY();
        int w = (int)r.getWidth();
        int h = (int)r.getHeight();

        p.setProperty("x", "" + x);
        p.setProperty("y", "" + y);
        p.setProperty("w", "" + w);
        p.setProperty("h", "" + h);

        BufferedWriter br = new BufferedWriter(new FileWriter(file));
        p.store(br, "Properties of the user frame");
    }

    /** Restore location & size of UI */
    public static void restoreOptions(Frame f) throws IOException {
        File file = new File(fileName);
        Properties p = new Properties();
        BufferedReader br = new BufferedReader(new FileReader(file));
        p.load(br);

        int x = Integer.parseInt(p.getProperty("x"));
        int y = Integer.parseInt(p.getProperty("y"));
        int w = Integer.parseInt(p.getProperty("w"));
        int h = Integer.parseInt(p.getProperty("h"));

        Rectangle r = new Rectangle(x,y,w,h);

        f.setBounds(r);
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame("Good Location & Size");
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        f.addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                try {
                    storeOptions(f);
                } catch(Exception e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }
        });
        JTextArea ta = new JTextArea(20,50);
        f.add(ta);
        f.pack();

        File optionsFile = new File(fileName);
        if (optionsFile.exists()) {
            try {
                restoreOptions(f);
            } catch(IOException ioe) {
                ioe.printStackTrace();
            }
        } else {
            f.setLocationByPlatform(true);
        }
        f.setVisible(true);
    }
}

I have three problems:

  1. I set fullscreen window (JFrame) and then I close it, when I reopen the window it has the dimensions of the fullscreen but it is not fullscreen.
  2. This 2° problem is not related to Thompson's code, when I move my window to screen borders it does not go behind the screen borders and stays side by side with the screen margin.
  3. Concerning to "option.prop" file, at the moment the file is stored in my .jar parent folder, I would like to store it in the same folder of my .jar program, how can I do it?

Thanks

Community
  • 1
  • 1
Frank
  • 2,083
  • 8
  • 34
  • 52
  • 1. Should be fixed by `// restore the frame from 'full screen' first! f.setExtendedState(Frame.NORMAL);` Are you saying it is not? 2. I do not understand what you are trying to say. 3. Put the options in a sub-directory of `user.home`, OS makers have long been saying not to save application settings in the same directory as the app. (or any of the parent directories). – Andrew Thompson May 27 '13 at 10:51
  • 1. yes I'm saying it is not, 2. when I move the frame of my program toward the screen borders it stays side by side to the border, instead normal programs hides over the screen borders. 3. ok I have red to put it in user.home but I can I do that? The user folder depends on the os. Thanks – Frank May 27 '13 at 11:00

1 Answers1

3

Here's a slightly modified example based on Andrew's.

Solves 1. and 3. (question comments taken into account). It keeps track of the frame's location and size before maximizing. Apparently calling f.setExtendedState(Frame.NORMAL) doesn't resize the frame back to original size immediately.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;
import java.io.*;

class RestoreMe {

    /**
     * This will end up in a sub-directory of user.home. 
     * (exercise done by the reader)
     */
    public static final String fileDir = 
            System.getProperty("user.home") 
            + System.getProperty("file.separator") 
            + ".restoreMe";
    public static final String fileName = 
            fileDir
            + System.getProperty("file.separator")
            + "props.file";


    /**
     * Store location & size of UI
     */
    public static void storeOptions(Frame f, Properties p) throws Exception {
        File file = new File(fileName);

        // only need to update extended state in properties
        p.setProperty("extState", String.valueOf(f.getExtendedState()));

        BufferedWriter br = new BufferedWriter(new FileWriter(file));
        p.store(br, "Properties of the user frame");
    }

    /**
     * Restore location & size of UI
     */
    public static void restoreOptions(Frame f, Properties p) throws IOException {
        File file = new File(fileName);
        BufferedReader br = new BufferedReader(new FileReader(file));
        p.load(br);

        int extState = Integer.parseInt(p.getProperty("extState"));

        int x = Integer.parseInt(p.getProperty("x"));
        int y = Integer.parseInt(p.getProperty("y"));
        int w = Integer.parseInt(p.getProperty("w"));
        int h = Integer.parseInt(p.getProperty("h"));

        Rectangle r = new Rectangle(x, y, w, h);
        f.setBounds(r);
        f.setExtendedState(extState);
    }

    public static void main(String[] args) {
        // we keep track of a single instance of properties
        final Properties p = new Properties();
        final JFrame f = new JFrame("Good Location & Size");
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                try {
                    storeOptions(f, p);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }            
        });
        // keep track of frame movement and update properties accordingly
        f.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                if (f.getExtendedState() != JFrame.MAXIMIZED_BOTH) {
                    Dimension d = f.getSize();
                    int w = (int) d.getWidth();
                    int h = (int) d.getHeight();                    
                    p.setProperty("w", "" + w);
                    p.setProperty("h", "" + h);
                }
            }
            @Override
            public void componentMoved(ComponentEvent e) {
                if (f.getExtendedState() != JFrame.MAXIMIZED_BOTH) {
                    Point l = f.getLocation();
                    int x = (int) l.getX();
                    int y = (int) l.getY();
                    p.setProperty("x", "" + x);
                    p.setProperty("y", "" + y);
                }
            }            
        });
        JTextArea ta = new JTextArea(20, 50);
        f.add(ta);
        f.pack();

        // create directory hierarchy for our app
        (new File(fileDir)).mkdirs();

        File optionsFile = new File(fileName);
        if (optionsFile.exists()) {
            try {
                restoreOptions(f, p);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } else {
            f.setLocationByPlatform(true);
        }
        f.setVisible(true);
    }
}

As for 2., you can't expect it to be answered, since you posted no code related to it. I'd suggest you create another question for that.

predi
  • 5,528
  • 32
  • 60