Re: Different height at JComboBox editor and renderer




Babu Kalakrishnan wrote:
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

Thank for pointing me in the right direction! However looking at the
source for the javax.swing.plaf.basic.BasicComboboxUI class, wouldn't I
need to subclass this class and overrided the getDisplaySize() method
since this is where takes the maximum size of either the editor or list
renderer?

.


Quantcast