JList : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

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:

This page is posted
on the web at:

http://mindprod.com/jgloss/jlist.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\jlist.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.133.108.172]
You are visitor number