Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixing the size of JScrollPane containing a JList

Status
Not open for further replies.

gregsa

Programmer
Jun 22, 2001
7
GB
I am trying to display just the selected row of a JList dropdown, but cannot seem to fix the size of the JScrollPane within which it is contained. The problem is that no matter what I do, the JScrollPane/JList combination is always larger than one row high. Could anyone explain to me how to limit the viewable portion of a JList to just one row?

I was originally using JComboBox, which worked fine as it doesn't need a JScrollPane, but I need to dynamically change the contents of the dropdown list, for which JList seems more suitable.

Thanks in advance,

Greg
 
Not too sure what effect you are trying to achieve but maybe setting the VisibleRowCount to 1 may help.

i.e. yourJList.setVisibleRowCount(1);
 
Thanks pipk.

I've tried that to no avail. It seems to have no effect.

To be more specific about the effect I'm after, I want the same effect achievable in html using <select><option><option>...</select>, where the resting state of the box is to show just one item. JComboBox does this when you use setMaximumRowCount(1), but it seems JList's setVisibleRowCount(1) doesn't work in the same way.

Currently I need to use JList as I need to frequently change the contents of the JList - I don't know if this can be done easily with JComboBox,

regards,

Greg
 
I assume that you mean that once you have clicked on the JList item, you want just that item to be displayed? hmmm, I think a combo box would be better as that functionality is built in. As for changing its contents, you could try

yourJComboBox.removeAllItems() and then use the addItem() method to readd new items one at a time (using a loop of course and bearing in mind you need to add objects such as Strings).

Not done this myself yet so not sure if it has the desired affect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top