aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "JComboBox" Watch "JComboBox" New topic
Author

JComboBox

Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
Hi,
How can I keep the height of a JComboBox unchanged when the main windows changes it size?


Thanks,<br />Mike
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.)


"I'm not back." - Bill Harding, Twister
Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
Hi Jim,
I used BoxLayout. The API docs says that the BoxLayout will give preferred height. But it still changing.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Nov 17, 2001
Posts: 175
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JComboBox
 
Similar Threads
Combo Box for flight origin and destination
drop down menu
How to restrict data in JTextField?
JComboBox Problem...Help
how to add dropdownlistbox to text field