Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox Height

 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lisa Zapson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it by setMaximumRowCount() on the combobox. Thanks for all your help. Lisa
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic