Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the size of a ContentPane (inside of a JFrame)

Tags:

swing

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!

like image 685
Jim Avatar asked May 09 '10 06:05

Jim


People also ask

How do I change the size of a JFrame?

Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.

How do I change the size of my Java Swing in Windows?

Using setSize() you can give the size of frame you want but if you use pack() , it will automatically change the size of the frames according to the size of components in it. It will not consider the size you have mentioned earlier.

What is the default size of a frame which is an object of JFrame class?

First a JFrame is created by invoking its constuctor. The argument to the constructor sets the title of the frame. The setSize(200,100) method makes the rectangular area 200 pixels wide by 100 pixels high. The default size of a frame is 0 by 0 pixels.

What is content pane in JFrame?

JFrames have a content pane, which holds the components. These components are sized and positioned by the layout manager when JFrame's pack() is called. Content pane border. There are several ways to handle the content pane, but most of them fail to provide one basic requirement -- ability to set a border.


1 Answers

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.

like image 120
Russ Hayward Avatar answered Sep 19 '22 21:09

Russ Hayward