JList : Java Glossary

JList
The Swing equivalent of java.awt.List. A JList is a list of choices from which you can select one — like a Choice continuously displayed already opened up. Commonly you embed a JList in a JScrollPane in case the list is too large to display all at once. Then you can use JList. getSelectedIndex() and JList. ensureIndexIsVisible( index ) to ensure the index you want is scrolled into view.
JComboBox is similar to JList, except JComboBox drops down where JList does not. Further, JList permits multiple selections where JComboBox does not.

A JList renders like this:

JList screen shot

Unlike a JComboBox, you cannot add items one by one to the list of available JList choices. There is no addItem method. You must create a new Object[] or Vector and replace the entire list with setListData. You can of course use a DefaultListModel which does give you the individual add methods.

Unlike JComboBox, JList does not notify the DataModel of selection changes.

You will get two ListSelectionEvents, one when the mouse button goes down and one when it releases. This is not considered a bug.

Oracle bug number 4323107 : JList generates two ListSelectionEvents per selection.

The removeAll method does not remove all the possible list selections. It is a vestigial method inherited from Container. Instead you must use DefaultListModel. clear.

What if the field you want to display is not the field the toString method displays from your data objects? You will need to create your own ListCellRender classe to select the field you want to display.

Learning More

Oracle’s Javadoc on JList class : available:
Oracle’s Javadoc on JScrollPane class : available:
Oracle’s Javadoc on DefaultListModel class : available:
Oracle’s Javadoc on ListModel interface : available:
Oracle’s Javadoc on ListSelectionEvent class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/jlist.html J:\mindprod\jgloss\jlist.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.211]
You are visitor number 24,351.