This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JComboBox Height Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox Height" Watch "JComboBox Height" New topic
Author

JComboBox Height

Lisa Zapson
Ranch Hand

Joined: Nov 22, 2004
Posts: 50
I am trying to modify the height of the dropdownlist in a JComboBox.

Any suggestions would be appreicated.
Thanks, Lisa
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Do you mean the height of each item, or the
total height? If the former, you can try this
ugly bit of code:

If the latter you can try calling jlist.setVisibleRowCount(someNumber) instead.


bitguru blog
Lisa Zapson
Ranch Hand

Joined: Nov 22, 2004
Posts: 50
Thanks for your help.

I am trying to make the height of the dropdown list longer to eliminate the scrollbar and see all the items in the list. I tried your example with jlist.setPreferredSize. However I got a longer list but I still need to scroll to see the last item. I did not succeed in getting rid of the scoll bar.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
setVisibleRowCount(int visibleRowCount)


Sets the preferred number of rows in the list that can be displayed without a scrollbar, as determined by the nearest JViewport ancestor, if any.

// Mathias


SCJP1.4
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Lisa Zapson:
I am trying to make the height of the dropdown list longer to eliminate the scrollbar and see all the items in the list. I tried your example with jlist.setPreferredSize. However I got a longer list but I still need to scroll to see the last item. I did not succeed in getting rid of the scoll bar.


Well setting the preferred size of the JList isn't likely to be
helpful since it's inside a JScrollPane. You could try to set
the preferred size of the scroll pane with something like this:

But that won't work either, for the same reason that
jlist.setVisibleRowCount() doesn't. (But we agree
that jlist.setFixedCellHeight() works, right?)

The reason is that the getPopupLocation() method in BasicComboPopup
calls setPreferredSize(), setMaximumSize(), and setMinimumSize()
on the scroll pane, which undoes our calls to setVisibleRowCount()
or setPreferredSize(). Or at least it probably does, depending
on your LnF.

So how do you get around this? I can think of two ways:

1) Replace the ComboPopup's JList with one where you have overridden
setPreferredSize() and setMaximumSize().

2) Subclass BasicComboBoxUI (or MetalComboBoxUI) and override
createPopup() to return a ComboPopup that doesn't set the scroll
pane's size (or at least sets its size to one you can live with).

Both are kind of ugly.
[ December 06, 2005: Message edited by: Brian Cole ]
Lisa Zapson
Ranch Hand

Joined: Nov 22, 2004
Posts: 50
I mean the total height. I have 9 items in the combobox and only eight show with a scrollbar. I would like all 9 to show without the scroll bar.

Thanks, Lisa
Lisa Zapson
Ranch Hand

Joined: Nov 22, 2004
Posts: 50
Please disregard my last reply, as I did not see the previous reply. I will try what was suggested.

Thanks, Lisa
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Brian Cole:
So how do you get around this? I can think of two ways:

1) Replace the ComboPopup's JList with one where you have overridden
setPreferredSize() and setMaximumSize().

2) Subclass BasicComboBoxUI (or MetalComboBoxUI) and override
createPopup() to return a ComboPopup that doesn't set the scroll
pane's size (or at least sets its size to one you can live with).


3) Register a PopupMenuListener that resets the size of the scroll
pane every time the user pops it up:

Still ugly, but it works. In the real world you would probably
want to make sure that d isn't too large or two small
before setting it as the scroll pane's size.

Of the three ways, I think (2) is the cleanest, but the problem
with that is you have to override a particular implementation of
ComboBoxUI, so the combo box get a hard-wired LnF, which is too bad.
Lisa Zapson
Ranch Hand

Joined: Nov 22, 2004
Posts: 50
I fixed it by setMaximumRowCount() on the combobox. Thanks for all your help. Lisa
 
GeeCON Prague 2014
 
subject: JComboBox Height