Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel width

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
-Shannon
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could always use the dreaded GridBagLayout.
 
Pat Hays
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for everyones help! I will try using each one of your suggestions. Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic