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
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).
> 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.