Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JComboBox: how to make drop down list width bigger than the combo width?

1,562 views
Skip to first unread message

David R

unread,
Oct 24, 2001, 5:58:43 PM10/24/01
to
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?

Appreciate any help.

Thanks,
David R

Christian Kaufhold

unread,
Oct 25, 2001, 2:41:28 PM10/25/01
to
Hello!

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

David R

unread,
Oct 26, 2001, 1:16:15 PM10/26/01
to
Hi:

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>...

Christian Kaufhold

unread,
Oct 26, 2001, 3:20:56 PM10/26/01
to
Hello!

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

Carlton S. Anderson

unread,
Oct 26, 2001, 5:22:25 PM10/26/01
to
Try this, gleaned from codeguru:

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

Linda Radecke

unread,
Oct 26, 2001, 6:12:31 PM10/26/01
to

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


0 new messages