JList : Java Glossary

go to home page J words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 Roedy Green, Canadian Mind Products
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.

Sun 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

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

Sun’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
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.108]
You are visitor number 19,360.