JFrame: get size without borders?
Asked Answered
B

4

28

In Java, is it possible to get the Width and Height of the JFrame without the title and other borders?

frame.getWidth() and frame.getHeight()1 seems to return the width including the border.

Thanks.

Briolette answered 23/2, 2011 at 21:21 Comment(0)
M
39

frame.getContentPane().getSize();

Mender answered 23/2, 2011 at 21:24 Comment(1)
Thanks! frame.pack(); should exist before this command. Otherwise, it is going to return (0; 0).Riojas
P
7
frame.pack();
System.out.println("frame width : "+getWidth());
System.out.println("frame height: "+getHeight());
System.out.println("content pane width : "+getContentPane().getWidth());
System.out.println("content pane height: "+getContentPane().getHeight());
System.out.println("width  of left + right  borders: "+(getWidth()-getContentPane ().getWidth()));
System.out.println("height of top  + bottom borders: "+(getHeight()-getContentPane().getHeight()));
Privity answered 4/9, 2012 at 20:1 Comment(0)
K
5

This works fine

frame.getContentPane().getSize();

But not if you haven't added content yet. In my case, I wanted to calculate the inner dimensions of the JFrame before adding content, so I could divide up the content accordingly. Here's what I came up with.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

pack(); // Need this, otherwise insets() show as 0.  
int scrW = (int)screenSize.getWidth();
int scrH = (int)screenSize.getHeight();
  int innerW = scrW - getInsets().left - getInsets().right;
  int innerH = scrH - getInsets().top - getInsets().bottom;

  // Need to setSize(), otherwise pack() will collapse the empty JFrame
  setSize(scrW, scrH);
Karyokinesis answered 21/4, 2014 at 13:5 Comment(0)
G
3

Here is a code snippet which works on JFrame as well as AWT's Frame (which happens to be the super-type of JFrame):

public static Dimension getInnerSize(Frame frame) {
    Dimension size = frame.getSize();
    Insets insets = frame.getInsets();
    if (insets != null) {
        size.height -= insets.top + insets.bottom;
        size.width -= insets.left + insets.right;
    }
    return size;
}

Beware: The insets are only valid once the frame has been shown.

Here is another code snippet to work around this problem:

private static Insets defaultInsets;
public static Insets getInsetsWithDefault(Frame frame) {
    // insets only correct after pack() and setVisible(true) has been
    // called, so we use some fallback strategies
    Insets insets = frame.getInsets();
    if (insets.top == 0) {
        insets = defaultInsets;
        if (insets == null) {
            insets = new Insets(26, 3, 3, 3);
            // usual values for windows as our last resort
            // but only as long as we never saw any real insets
        }
    } else if (defaultInsets == null) {
        defaultInsets = (Insets) insets.clone();
    }
    return insets;
}

This code needs to be called once with a visible Frame. After that it can correctly predict the insets even for invisible frames (due to caching of the defaultInsets), assuming they are always the same.

Of course this only works if all windows get the same window-decorations. But i am not aware of any case where they might differ from window to window.

This might be useful, too:

frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowOpened(WindowEvent e) {
        MyUtilClass.getInsetsWithDefault(frame); // init the defaultInsets
    }

});

It will call the getInsetsWithDefault() method once the window is visible and initialize the correct defaultInsets.

Gaffrigged answered 17/8, 2018 at 12:10 Comment(1)
It appears that you do not need to show the frame to get the insets; you just need to pack() the frame.Ellenaellender

© 2022 - 2024 — McMap. All rights reserved.