Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for setting JFrame locations

I have a (somewhat philosophical) question relatively to Swing, or to GUI programming in general. Are there recognized best practices on where to locate the JFrame instances used in the application?

  1. Where should the first and main frame be located? Always at the center (setLocationRelativeTo(null))?
  2. Where should a child JFrame be located? Relatively to its parent JFrame, at the center of the screen, wherever we want?

I have always assumed there were some best practices, kind of a "GUI bible" about this, am I wrong and should I (gasp) arbitrarily decide what to do?

like image 287
Scherrer Vincent Avatar asked Oct 15 '11 11:10

Scherrer Vincent


People also ask

How set JFrame location on screen?

By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.

What is set location in Java?

SWT Positioning – setBounds() or setLocation() In SWT, we can use setLocation() or setLocation() method to specify the size and position of a widget or component. Here is the two methods that SWT use to positioning. 1) setBounds(int x, int y, int witdh, int height) – Sets the widget's size and location.

Which method of JFrame sets the title of the frame?

First, you can set the JFrame title when you construct your JFrame, like this: JFrame jframe = new JFrame("My JFrame Title"); Second, once you have a valid JFrame object, you can call the setTitle method of the JFrame class, like this: jframe.


2 Answers

Here is an example that incorporates the advice of:

  1. Hovercraft Full Of Eels - set location by platform.

  2. Aardvocate Akintayo Olu - serialize the location.

But goes on to add 2 tweaks:

  1. Serialize the width/height as well.
  2. If the frame is maximized at time of close, it is restored before getting the bounds. (I detest apps. that serialize options but do not take that into account. The user is sitting there clicking the 'Maximize / Restore' button & wondering why nothing is happening!)

The 4 points combined offer the best user experience!

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);
    }
}
like image 119
Andrew Thompson Avatar answered Oct 16 '22 07:10

Andrew Thompson


I've usually let the platform decide by calling:

myJFrame.setLocationByPlatform(true);

This lets the window "appear at the default location for the native windowing system". For more on this: Window API

like image 34
Hovercraft Full Of Eels Avatar answered Oct 16 '22 08:10

Hovercraft Full Of Eels