views:

260

answers:

1

The problem: I have a combo box that needs to fit into a fixed space as part of a Swing application. However, its contents may be quite long. I'd like the box itself to be a fixed size, truncating contents. When the down arrow is clicked, though, I'd like it to act similar to an HTML select and show a box that is long enough to fit the longest entry as the drop down. The ListCellRenderer may be the way to go on this one; I'm not sure.

It also may be that I need some sort of implementation of javax.swing.plaf.basic.ComboPopup and my own ComboBoxUI. I've dug through the SwingUtilities code to understand how the DefaultListCellRenderer does its calculations. It uses a JLabel, and the BasicLabelUI calls SwingUtilities.layoutCompoundLabel (eventually in the call stack) which does the clipping. BasicComboPopup, the only implementation of ComboPopup in the Java 6 code I was digging through, seemed to delegate to: JList.computeVisibleRect(Component c, Rectangle visibleRect)

Has anyone done this before? Any pointers?

+1  A: 

Here is a good starting point :)

http://www.crionics.com/products/opensource/faq/swing_ex/JComboBoxExamples1.html

eugener