|
The common question "How do I create a JComboBox with a horizontal scrollbar?", seems to be regarded as bad UI technique by sun http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5091278.
So, what happens if we need that?
Actually, I agree that having scrollbars on a combobox list is extremelly disconcerting. But still, there is a problem related to the lack of control about sizes.
Edit a code but show a description
My main concern is how to show a list with descriptions, but present a code on the combo editor. The problem is that the list is always the same size of the combo, but a simple trick can solve it, just redefine the getSize() method:
public Dimension getSize() {
return ui.getPreferredSize(this);
}
the getSize is not called very often, at least not with the common layout managers and seems to be safely ovewriten.
Now, to how the code of the item instead of the description, we just need to overwrite the configureEditor() method:
public void configureEditor(ComboBoxEditor anEditor, Object anItem) {
if (anItem != null) {
super.configureEditor(anEditor, ((MyBean) anItem).getCode());
setToolTipText(((MyBean) anItem).getDescription());
} else {
super.configureEditor(anEditor, anItem);
setToolTipText("Carried code...");
}
}
|