A JList displays a list of options that a user can select. Unlike a JComboBox, all of the options are displayed at all times.
String[] items = new String[] { "item 1", "item 2", "item 3", "item 4" }; JList list = new JList(items);
list
can be wrapped in a JScrollPane with the
following code.
JScrollPane scrollPane = new JScrollPane(list);When this is done,
scrollPane
should be added to a container
instead of list
.
Describe model and model access methods.
A JList fires a ListSelectionEvent whenever the user changes the selection in the list.
list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { code to implement response to selection change. } });