Re: Different height at JComboBox editor and renderer




Beaned wrote:
I have a combo box which uses a custom ListCellRenderer which basically
displays two lines of text (the preferred name of an object and an
alternate name of the object). That works well. The problem is that the
editor for the combobox (which is editable) is being displayed with the
same height as the ListCellRenderer (ie two lines) but I would like it
to be normal and be a single text line height. I can't figure out how
to do this. Is this possible? If so could you please provide an example
of how it is done?


The issue is basically - how to inform the JCombobox as to what
"normal" height is :-)

The default strategy of an editable JComboBox in finding its preferred
size is to check for the preferredSize of its renderer when rendering
each item, as well as the preferredSize of its editor component, and
choose the maximum of these along both the width and the height, (Check
out the source code of the getPreferredSize(Component) /
getMinimumSize(Component) methods in the
javax.swing.plaf.basic.BasicComboboxUI class).

If you have something better to suggest as the preferred size (or some
alternate strategy to compute it) you could either override its
getPreferredSize() method (recommended if your size needs to change
dynamically) or to compute it once and call setPreferredSize() on it
(if you don't anticipate the size to change after the component has
been created). Swing will then honour the value returned by the
getPreferredSize() method to fix the size fo the combobox.

BK

.



Relevant Pages

  • Re: Different height at JComboBox editor and renderer
    ... editor for the combobox is being displayed with the ... size is to check for the preferredSize of its renderer when rendering ... getPreferredSize() method to fix the size fo the combobox. ...
    (comp.lang.java.gui)
  • Re: Different height at JComboBox editor and renderer
    ... editor for the combobox is being displayed with the ... size is to check for the preferredSize of its renderer when rendering ... By subclassing BasicComboBoxUI I loose ...
    (comp.lang.java.gui)
  • Re: Why does this work for Canvas but not for JPanel?
    ... >>You should use the setPreferredSize to set a preferred size. ... noticed that this is also a difference between Canvas and JPanel. ... > also tried overriding getPreferredSize and this seems to work as well. ... you must also remember that setting the preferredSize to ...
    (comp.lang.java.programmer)
  • Re: Component preferred size dilemma
    ... > getPreferredSize() to find the preferred width, ... Are you sure this happens with a JTextField? ... Integer.MAX_VALUE on either axis and computes the preferredSize. ... For components that can wrap text (such as the JTextArea), ...
    (comp.lang.java.programmer)