0

It seems that the JComboBox is one Java component that really, really hates having its height adjusted...I tried countless combinations of set[Preferred|Minimum|Maximum]Size() and various different layout managers until the following GroupLayout code finally worked:

JComboBox cmbCategories = new JComboBox(new String[] { "Category 1", "Category 2" });
...
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addComponent(cmbCategories, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
...
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
        .addComponent(cmbCategories, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)

But I've now switched over to JGoodies FormLayout which once again refuses to resize the damn combobox! I've currently got the following code:

JPanel contentPane = new JPanel();
contentPane.setLayout(new FormLayout("50dlu, $lcgap, 110dlu, $glue, " +
    "default, 1dlu, 45dlu, 1dlu, 45dlu", "2*(default, 0dlu), default, " +
    "$lgap, fill:30dlu, $lgap, default:grow"));
...
contentPane.add(cmbPanel, CC.xy(1, 7, CC.FILL, CC.FILL));

which displays what I want in the JFormDesigner Editor, but upon running the program it simply gets set back to the default!

So what kind of magical hocus-pocus do I need to conjure up to get this to work?! I really don't want to have to go back to defining everything twice in a GroupLayout, but after 5 hours of trying to resize a damn combobox I'm on the verge of baldness!

MTIA to anyone that can help :)

Kenny83
  • 769
  • 12
  • 38
  • Ended up solving it myself by explicitly setting the look and feel I wanted. Thanks anyway guys :-) – Kenny83 Oct 26 '14 at 04:18

1 Answers1

2

First of all we have to avoid setting hard-coded sizes in our components, because Swing is designed to be used with Layout Managers and our application must be capable to be executed in different platforms, different screen resolutions, different PLaF's and different font sizes. Components sizing and positioning is layout managers' responsibility not developers'.

Now, generally speaking when we want to set a preferred size to our Swing components we don't use any of setXxxSize() methods but do override getPreferredSize() method instead:

JComboBox comboBox = new JComboBox() {
    @Override
    public Dimension getPreferredSize() {
        return isPreferredSizeSet() ? 
                super.getPreferredSize() : new Dimension(100, 40);
    }
};

However doing this doesn´t affect the size of the items listed when the pop up becomes visible: cells still having the preferred size determined by the combo box cell renderer. So, to avoid this undesirable behavior, a better solution would be:

For example:

JComboBox comboBox = new JComboBox();
comboBox.setPrototypeDisplayValue("This is a cell's prototype text");
comboBox.setRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        int width = c.getPreferredSize().width; // let the preferred width based on prototype value
        int height = 40;
        c.setPreferredSize(new Dimension(width, height));
        return c;
    }
});

Once again I'd like to emphasize that this is kind of hacky/dirty way to size our combo box. IMHO it would be better don't mess with combo box height at all and just play with setPrototypeDisplayValue(...) to set a preferred width in a PLaF safe way.

dic19
  • 17,821
  • 6
  • 40
  • 69
  • Thanks anyway but I've tried both of your suggestions to no avail...I agree that this is incredibly hacky/dirty but I **need** my combobox to be at least 40 pixels high **no matter what**...I've spoken to my teacher again (this is a uni assignment) and he isn't willing to budge at all on this issue...so what the heck do I have to do?! Use a `GroupLayout`ed panel inside the `FormLayout`?? Now **that's** what I call **really** hacky! – Kenny83 Oct 26 '14 at 01:13