Setting the size of a ContentPane (inside of a JFrame)
Asked Answered
M

2

10

I want to set the size of a JFrame such that the contentPane is the desired size. JFrame.setSize() doesn't take the window decorations into account, so the contentPane is slightly too small. The size of the window decorations are platform and theme specific, so it's bad news to try to manually account for them.

JFrame.getContentPane().setSize() fails because it's managed.

Ideas?

Thanks!

Mayman answered 9/5, 2010 at 6:1 Comment(1)
This question may help: #2451752Individuality
W
22

In Java 5 and later this is the easiest method:

JFrame frame = new JFrame("Content Pane Size Example");
frame.getContentPane().setPreferredSize(new Dimension(400, 300));
frame.pack();
frame.setVisible(true);

As always the above should be executed in the EDT.

Whereat answered 9/5, 2010 at 10:17 Comment(0)
O
0

If you want to do it on your own then in your Frame type:

this.setLayour(null);
this.pack();
Insets insets = this.getInsets();
this.setBounds(0,0, insets.left + width + insets.right, insets.top + height + insets.bottom);
this.setLocationRelativeTo(null);
Oswell answered 30/8, 2015 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.