• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How can I keep the height of a JComboBox unchanged when the main windows changes it size?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually it stays unchanged automatically, with no special effort required. But some layout managers will change the height in some situations. The main example I know is BorderLayout - if you put a component in WEST or EAST its height will change, but its width will not; if you put the component in NORTH or SOUTH its width will change, but not height. If you put the component in CENTER (which is the default of you don't specify a constraint string) then both width and height will change. So probably the solution is to use a different layout manager for the field, or to put the JComboBox in NORTH or SOUTH. You might consider using Box.createHorizontalBox(), or a GridBagLayout with constraints weightX = 1, weightY = 0, fill = HORIZONTAL. (No vertical stretching.)
 
Mike Yu
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
I used BoxLayout. The API docs says that the BoxLayout will give preferred height. But it still changing.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm - you're using a horizontal box? I see it says that if there are other components in the box, the layout will attempt to stretch components vertically to match the height of the tallest component. Try using setMaximumSize() on the JComboBox to prevent it from getting any taller. Something like:
Dimension pref = jcb.getPreferredSize();
Dimension max = new Dimension(Integer.MAX_VALUE, pref.height);
jcb.setMaximumSize(max);
where jcb is the JComboBox.
Or, try using another layout manager (if that can make sense for whatever layout you want). Or post some more sample code showing how the JComboBox is put into the layout with other components. Good luck...
 
Mike Yu
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
I use a vertical BoxLayout for a JPanel.
I tried to use max = new Dimension(Integer.MAX_VALUE, pref.height);
The height is fine, but the width became much wider than I want.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can set maximum width in much the same way you set maximum height. (In fact, you can do it in the same code already shown.) Just look at the documentation for the new Dimension() constructor to see what each parameter does.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic