I am first adding some elements to a JPanel
and then adding the JPanel
to the contentPane of the JFrame
. However, the elements I add to the JPanel are all not fitting inside the Jframe. How do I ensure that all elements inside the JPanel
are visible inside the JFrame
Asked
Active
Viewed 4,571 times
2

Andrew Thompson
- 168,117
- 40
- 217
- 433

Programmer
- 6,565
- 25
- 78
- 125
-
1You'll want to read the tutorials on the layout managers available so that you can get to know them quite well as they are the key to "fitting" and sizing your GUI appropriately. Then yes, call `pack()` as @kajacx states below (1+ to him). – Hovercraft Full Of Eels Feb 21 '12 at 18:59
1 Answers
6
try calling the pack() method on the frame after adding the panel, like
myFrame.add(myPanel);
myFrame.pack();
this should fit the frame's size to it's components.

kajacx
- 12,361
- 5
- 43
- 70
-
1I tried this but now my entire window is just the taskbar :(....cant see anything – Programmer Feb 21 '12 at 19:05
-
strange, are you using any layout managers? Last time I used BorderLayout and pack() worked fine. True, i had only pre-defined components (buttons and labels) and no panels. – kajacx Feb 21 '12 at 19:21
-
since you av only one jpanel inside a jframe content pane, trying using a box layout, or grid layout. it will fit right in. – maress Feb 21 '12 at 19:28
-
4He needs to read the layout manager tutorials as has been recommended more than 2 hours ago. Find them here: [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html). – Hovercraft Full Of Eels Feb 21 '12 at 21:04
-
1@Programmer: If you're adding your own component(s) to the frame, you may need to override `getPreferredSize()` as discussed [here](http://stackoverflow.com/a/7229662/230513). – trashgod Feb 22 '12 at 02:53
-
@HovercraftFullOfEels enough RTFM already! some of us need answers! – necromancer May 14 '13 at 02:01
-
@randomstring: you think that there's an easy way to learn all of this stuff? Sorry to say but there are no short cuts, and the sooner you learn that and RTFM, the sooner you'll become a real programmer. – Hovercraft Full Of Eels May 14 '13 at 02:08
-
@HovercraftFullOfEels dude, i just need to plot a few points today to debug something and then for the next couple of years i won't need to touch this again and perhaps for ever because the web browser is where the action is (don't bring up javafx please). if you think one should spend hours learning swing just to plot a few points on a one-off basis it is more of a reflection of how misguided your own programming priorities are. jeez. – necromancer May 14 '13 at 02:11
-
@HovercraftFullOfEels you are simply trolling around completely unhelpfully. everybody knows there are tutorials and manuals. we all have our domains of expertise where we have invested and it helps to not have everybody bear the significant fixed cost of learning everything. – necromancer May 14 '13 at 02:13