Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the real size of a JFrame content

Tags:

java

swing

I get a JFrame and i want to display a JLabel with a border in it with a padding of maybe 50px. When i set the size of the JFrame to 750, 750, and the size of the JLabel to 650, 650 and the location to 50, 50, it display it strange... Here's my code:

public class GUI {

    /**
     * Declarate all 
     */
    public int height = 750;
    public int width = 750;

    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screen.width / 2) - (width / 2); // Center horizontally.
    int y = (screen.height / 2) - (height / 2); // Center vertically.

    /**
     * Create the GUI
     */
    JFrame frame = new JFrame();
    Border border = LineBorder.createBlackLineBorder();
    JLabel label = new JLabel();    

    public GUI(){
        label.setBorder(border);
        label.setSize(700, 700);
        label.setLocation(0, 0);

        frame.getContentPane().setLayout(null);
        frame.add(label);
    }

    public void createGUI() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(x,y,width,height);
        frame.setVisible(true);
    }

}

So I think the title-bar at the top is also include in the size. In Graphics you can use getInsets(). Now is there anything like that for Swing / JFrame?

like image 279
Michael Schmidt Avatar asked Nov 20 '12 13:11

Michael Schmidt


2 Answers

First get the pixels trimmed out by the frame.

int reqWidth = reqHeight = 750;

// first set the size
frame.setSize(reqWidth, reqHeight);

// This is not the actual-sized frame. get the actual size
Dimension actualSize = frame.getContentPane().getSize();

int extraW = reqWidth - actualSize.width;
int extraH = reqHeight - actualSize.height;

// Now set the size.
frame.setSize(reqWidth + extraW, reqHeight + extraH);

An alternate simpler way. The previous works but this is recommended.

frame.getContentPane().setPreferredSize(750, 750);
frame.pack();

Hope this helps.

EDIT:

Add this in your constructor before adding components to the frame. and to set it in the middle, use

frame.setLocationRelativeTo(null);

This will center the window on the screen.

like image 56
Sri Harsha Chilakapati Avatar answered Oct 26 '22 12:10

Sri Harsha Chilakapati


Using setPreferredSize() is problematic, as it always overrules the component's calculation with an arbitrary choice. Instead, pack() the enclosing Window to accommodate the preferred sized of the components, as shown below.

image

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;

/**
 * @see https://stackoverflow.com/a/13481075/230513
 */
public class NewGUI extends JPanel {

    private static final int S1 = 10;
    private static final int S2 = 50;
    private JLabel label = new JLabel("Hello, world!");

    public NewGUI() {
        label.setHorizontalAlignment(JLabel.CENTER);
        Border inner = BorderFactory.createEmptyBorder(S1, S1, S1, S1);
        Border outer = BorderFactory.createLineBorder(Color.black);
        label.setBorder(new CompoundBorder(outer, inner));
        this.setBorder(BorderFactory.createEmptyBorder(S2, S2, S2, S2));
        this.add(label);
    }

    private void display() {
        JFrame f = new JFrame("NewGUI");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NewGUI().display();
            }
        });
    }
}
like image 20
trashgod Avatar answered Oct 26 '22 12:10

trashgod