Java Swing set "actual" Frame size (inside)

CookieMonster picture CookieMonster · May 6, 2011 · Viewed 20.3k times · Source

I have a JFrame which contains just one JPanel. I have tried setting the Panel's size and packing the frame, but that has no effect.

If I set the JFrame's size, it will change the size so it includes the title bar and borders. How do I set the "actual size" so it doesn't include the title bar and borders?

Example:

Example

Thanks in advance, guys

Answer

Hovercraft Full Of Eels picture Hovercraft Full Of Eels · May 6, 2011

You could set the contentPane's preferredSize and call pack on the JFrame, but in general it's usually best to let components size themselves based on their preferred sizes as determined by their layout managers.