3

If you don't know what "AdjustWidowRect" does, here's a description from MSDN:

Calculates the required size of the window rectangle, based on the desired client-rectangle size.

More clearly:

In swing, when you set the size of a JFrame, that includes the border. That means that if you set the size of the JFrame to 640 by 480, that will not be the client size, since the size you entered counts the size of the frames' border.

I want to have a rectangle, and be able to adjust it so when the JFrame's size is set to that rectangle, the client size of the JFrame is what the rectangle was before adjustment.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Name
  • 2,037
  • 3
  • 19
  • 28

2 Answers2

3

You have to compute the insets of the JFrame and add them to the desired client size, to set the size of the JFrame.

Insets insets = getInsets();
PhiLho
  • 40,535
  • 6
  • 96
  • 134
2

AFAIU the accepted answer does not account for menu bars or other components in the mix. This does, by overriding the preferred size of the component, and packing the frame.

I'm drawing graphics to the JFrame and I need it to be a precise size.

Don't paint to a top level container such as JFrame or JWindow. Instead render to a JPanel or BufferedImage and add it to the TLC.

Sized GUI - from the 'inside out'

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SizedGUI {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(2,3,2,3));

                gui.add(new FixedSizeComponent());

                gui.setBackground(Color.RED);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // tweak to ensure the GUI never gets too small
                f.setMinimumSize(f.getSize());
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

class FixedSizeComponent extends JPanel {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400,100);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int w = getWidth();
        int h = getHeight();
        g.setColor(Color.BLACK);
        g.drawString(w + "x" + h, w/2, h/2);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Well I cannot accept as I already did the other one, but I will certainly make use of your answer, +1 thanks alot! – Name Dec 13 '12 at 20:30
  • *"I cannot accept as I already did the other one"* There is always 'unaccept'. ;) – Andrew Thompson Dec 14 '12 at 00:58
  • Sorry I know but I gave the guy the accept I dont really think its right to just take it away... – Name Dec 14 '12 at 21:08
  • I will add nothing more beyond the fact that that is how the site is intended to work. If it was not, we would not be able to change an 'accept'. – Andrew Thompson Dec 15 '12 at 05:53