Make the component occupy the minimum size

I have a JFrame whose layout manager is TableLayout. I inside one of the cells of the TableLayout I have a JComboBox. My problem is, the JComboBox is expanding to occupy the entire width of the cell, instead of taking up only the least amount of space necessary. I am setting the JComboBox's size to be the minimum by calling setMaiximumSize(getPreferredSize()) and setMinimumSize(getPreferredSize()).

How can I make the cell in the TableLayout not extend the JComboBox? How can I shrink the cell to the true size of the JComboBox?

Thanks

[558 byte] By [ogami_ittoa] at [2007-10-2 4:36:51]
# 1
Try adding the combobox to a JPanel with the default FlowLayout, and then add that panel to the TableLayout.
happy_hippoa at 2007-7-16 0:09:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

I mean like this:

JFrame frame = ... // frame with TableLayout

JComboBox combo = ....

JPanel comboCapsule = new JPanel(); // FlowLayout per default

comboCapsule.add(combo);

frame.add(comboCapsule); // add the combo capsule rather than the combo directly

This way the comboCapsule panel will get all the available space, but the combobox itself will be restricted to its preferred size (as per the FlowLayout contract).

happy_hippoa at 2007-7-16 0:09:28 > top of Java-index,Desktop,Core GUI APIs...
# 3
Yes, but now I have a tiny JComboBox in the middle of a sea of white space.No more clues, folks?
ogami_ittoa at 2007-7-16 0:09:28 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Yes, but now I have a tiny JComboBox in the middle of

> a sea of white space.

> No more clues, folks?

Wasn't your request to make the combobox take up as little space as possible?!

To control the preferred size of the combo you might try setting its prototype value:

combo.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXX");

That will give the combobox a hint of how much size it needs to display whatever it will contain.

happy_hippoa at 2007-7-16 0:09:28 > top of Java-index,Desktop,Core GUI APIs...
# 5
I am sorry, I didnt explain myself properly.After checking the documentation for TableLayout I found out how to do it. I am going to specify the width of the cell in terms of percentage of screen real estate taken.
ogami_ittoa at 2007-7-16 0:09:28 > top of Java-index,Desktop,Core GUI APIs...