The setSize() method of a Frame only resizes the frame itself, not the canvas inside. Is there a way to resize a frame based on what size you want the interior to be?

I made this image to explain what I'm talking about: I used setSize(500,500);

I suppose I could count pixels, but that stuff is platform-dependent, and plus it's not elegant :P

Dani AI

Generated

Nice work getting to the root of it — was right that pack() + preferred size is the clean approach, and ‘s final post shows the practical fix for JFrame. A few clarifications and safer, reusable patterns that avoid platform-dependent pixel counting:

Create your UI on the Event Dispatch Thread and let components supply their preferred size (override getPreferredSize on a custom painting component) rather than repeatedly calling setPreferredSize on the top-level container. Example pattern:

class CanvasPanel extends JComponent {
    @Override
    public Dimension getPreferredSize() { return new Dimension(500, 500); }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // paint here
    }
}

// In EDT:
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new CanvasPanel());
frame.pack();                 // sizes frame to component preferred size
frame.setLocation(100, 100);
frame.setVisible(true);

If you must compute the outer (decorated) window size directly (e.g., when mixing AWT Frame or when you need an exact client area but cannot rely on preferred sizes), read the decoration Insets after the window is displayable and add them to the desired client dimensions:

frame.pack();                 // ensure peer exists, or call addNotify()
Insets in = frame.getInsets();
int outerW = desiredClientW + in.left + in.right;
int outerH = desiredClientH + in.top + in.bottom;
frame.setSize(outerW, outerH);

Troubleshooting notes: call pack() or addNotify() before reading insets, be aware look-and-feel and OS chrome change insets, and if you want a fixed client area make the frame non-resizable. ‘s remark about layout managers is important: pack only works if components report sensible preferred sizes, so prefer overriding getPreferredSize over hard-coding sizes in many places.

Recommended Answers

All 5 Replies

i usualy set the prefered size of the canvas to the size desired, then call up the pack() method.

Unfortuately that doesn't work either :(

It depends on yout layout manager. You may need to call SetPreferredSize, setMinimumSize, setMaximumSize and/or setSize before calling pack. But is does work.

Is this the right way to approach it?

void createFrame(int[] location, int[] size){
        Frame f = new Frame();
        f.setLocation(location[0],location[1]);
        f.setPreferredSize(new Dimension(size[0],size[1]));
        f.setMinimumSize(new Dimension(size[0],size[1]));
        f.setMaximumSize(new Dimension(size[0],size[1]));
        f.setSize(size[0],size[1]);
        f.pack();
        f.setVisible(true);
}

Nevermind, found a way that works using your advice. Thanks for the help, guys!

Solution:

void createFrame(int[] location, int[] size) {
        JFrame f = new JFrame();
        f.setLocation(location[0],location[1]);
        f.getContentPane().setPreferredSize(new Dimension(size[0],size[1]));
        f.pack();
        f.setVisible(true);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.