This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JPanel width Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel width" Watch "JPanel width" New topic

JPanel width

Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Does anyone know how to change the width of a panel? I tried using:

JPanel panel = new JPanel();
panel.setPreferredSize( new Dimension( 15, 15 ) );

however, this does not seem to change the dimension of my panel. I'm not sure if this makes any difference, but this panel is in a GridLayout. Perhaps I should be asking how do I change the dimension of the GridLayout? I have 3 rows and I would like the 1 row to be narrower than the last 2 rows.

Thanks for your help!
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Check the GridLayout api to see (in the first paragraph) that the grid is made so all cells are the same size. If you want a component to be shown at it's preferred size, like (15,15), add it to a JPanel and add the JPanel to the GridLayout. The JPanel will expand to fill the gird cell and allow the component to be shown at it's preferred size.

For a row of different size you'll have to try something else. Nested layouts would be a good starting place...
Warren Dew
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
You could always use the dreaded GridBagLayout.
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
Another way:
you could use multiple JPanels which may be set PreferredSize, LayoutManager individually in your main Panel. it would be easer than using the dreaded GridBagLayout.

Download Java GUI Builder, <a href="" target="_blank" rel="nofollow"></a>
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Thanks for everyones help! I will try using each one of your suggestions. Thanks again.
I agree. Here's the link:
subject: JPanel width
It's not a secret anymore!