How can I make the drop-down list wide enough to show my widest items,
but at the same time keep the combo box short?
Appreciate any help.
Thanks,
David R
David R <dav...@advinfo.net> wrote:
> I am trying to use JComboBox with a list of long text items. In the
> selection box, I do not want to display the entire text (due to screen
> size constraints).
> How can I make the drop-down list wide enough to show my widest items,
> but at the same time keep the combo box short?
Change the cellRenderer: if the given index is -1, the item in the
box is shown; if it is != -1, the renderer is queried for the item
in the popup.
Christian
Thanks for your feed back, but I am not able to use this. Would you
please send some sample code.
cellRenderer is not a property of JComboBox and I am not sure what
value I need to set it at to make the drop down list wider or
scrollable.
Thanks again!
David R
use...@chka.de (Christian Kaufhold) wrote in message news:<3t3bd85c...@simia.chka.de>...
David R <dav...@advinfo.net> wrote:
> cellRenderer is not a property of JComboBox and I am not sure what
> value I need to set it at to make the drop down list wider or
> scrollable.
You can't (with the standard UIs). These always make the popup only
as wide as the combo box. I confused this with the height of each cell,
which you can configure differently (Is it an option to wrap the items
in the popup?).
Christian
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
/** A ComboBox that correctly renders when used in a table.
**
** @author Nobuo Tamemasa, codeguru.earthweb.com
*/
public class SteppedComboBox extends JComboBox
{
/** The width needed to display the ComboBox's popup window. */
protected int popupWidth;
/** Create a ComboBox using the given model. */
public SteppedComboBox (ComboBoxModel aModel)
{
super (aModel);
setUI ( new SteppedComboBoxUI () );
popupWidth = 0;
}
// end constructor with a ComboBoxModel
/** Create a ComboBox using the given array. */
public SteppedComboBox (final Object [] items)
{
super (items);
setUI ( new SteppedComboBoxUI () );
popupWidth = 0;
}
// end constructor with an Object array
/** Create a ComboBox using the given vector. */
public SteppedComboBox (Vector items)
{
super (items);
setUI ( new SteppedComboBoxUI () );
popupWidth = 0;
}
// end constructor with a Vector
/** Assign the width of this ComboBox for displaying in a popup
window. */
public void setPopupWidth (int width) { popupWidth = width; }
/** Determine the size necessary to display this ComboBox in a popup
window.
*/
public Dimension getPopupSize ()
{
Dimension size = getSize ();
if (popupWidth < 1)
{
popupWidth = size.width;
}
return ( new Dimension (popupWidth, size.height) );
}
// end method setPopupWidth
/** The UI for this ComboBox. */
private class SteppedComboBoxUI extends MetalComboBoxUI
{
/** Draw the ComboBox. */
protected ComboPopup createPopup ()
{
BasicComboPopup popup = new BasicComboPopup (comboBox)
{
public void show ()
{
Dimension popupSize =
( (SteppedComboBox) comboBox ) . getPopupSize
();
popupSize.setSize ( popupSize.width,
getPopupHeightForRowCount
( comboBox.getMaximumRowCount () ) );
Rectangle popupBounds = computePopupBounds
(0, comboBox.getBounds ().height,
popupSize.width, popupSize.height);
scroller.setMaximumSize ( popupBounds.getSize () );
scroller.setPreferredSize ( popupBounds.getSize
() );
scroller.setMinimumSize ( popupBounds.getSize () );
list.invalidate ();
int selectedIndex = comboBox.getSelectedIndex
();
if (selectedIndex == -1)
{
list.clearSelection ();
}
else
{
list.setSelectedIndex (selectedIndex);
}
list.ensureIndexIsVisible ( list.getSelectedIndex
() );
setLightWeightPopupEnabled
( comboBox.isLightWeightPopupEnabled () );
show (comboBox, popupBounds.x, popupBounds.y);
}
};
popup.getAccessibleContext ().setAccessibleParent
(comboBox);
return (popup);
}
// end method createPopup
}
// end internal class SteppedComboBoxUI
}
// end class SteppedComboBox
--
------------------------------------------------------------------karlik
David R wrote:
> I am trying to use JComboBox with a list of long text items. In the
> selection box, I do not want to display the entire text (due to screen
> size constraints).
> How can I make the drop-down list wide enough to show my widest items,
> but at the same time keep the combo box short?
Actually this is not possible by standard, you will need to implement
that by extending MetalComboBoxUI, IMO. I have found an example in the
web, which does something like that, although I have not tested it:
http://www2.gol.com/users/tame/swing/examples/JComboBoxExamples1.html
(Perhaps it is even the same as another poster alreay has posted)
Linda
--
Hello darkness, my old friend / I've come to talk to you again
Because a vision softly creeping / Left its seed while I was
sleeping / And a vision, that was planted in my brain / Still
remains within the Sound of silence Simon & Garfunkel