I have a JFrame that has a JPanel inside. I call "setPreferredSize(new Dimension(500, 600));" but I want the JPanel and its contents to resize when someone resizes the JFrame.
Asked
Active
Viewed 145 times
0
-
2remove `setPreferredSize(new Dimension(500, 600));` and leave to layout manager to set the size of `JPanel`. – Braj Apr 16 '14 at 20:31
-
What layout are you using for `JPanel` and `JFrame`? – Braj Apr 16 '14 at 20:32
-
1Please have a look at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – Braj Apr 16 '14 at 20:33
-
[Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Apr 16 '14 at 20:34
3 Answers
1
BorderLayout is the way to go. Components start at their preferred size, but are expanded as needed to fill the region they are in.

Abu Sulaiman
- 1,477
- 2
- 18
- 32
0
Set your layout on your frame with BorderLayout Add your JPanel by
frame.add(yourPanel, BorderLayout.CENTER);
This will allow it to stretch vertically & horizontally
As for the Contents inside a JPanel, give it a layout that will accommodate stretching as well.

user121449
- 15
- 2
0
Use a layout manager instead of setting the bounds for each component.
It is going to vary from program to program how you want your components to move.
Take a look at this and try to see which layout will work best for you.

ShihabSoft
- 835
- 6
- 15