JList
- A component that allows the user to select one or more objects
from a list. A separate model,
ListModel
, represents the
contents of the list.
- JList and ListModel work together to maintain the list.
JList is the visual interface item, ListModel is used to maintain
the list, this includes adding items, removing items, and getting item
counts.
- Since ListModel is an interface class, you have to use a class
that implements it. For most lists, you can use a class called
DefaultListModel.
- To create in Netbeans:
- Since you'll probably want to have scroll bars on the list, add
a JScrollPane to your JPanel, then add the JList to it. (version 5.5+
of
Netbeans does this for you automatically when you add a JList
- Rename the JList to something logical.
- Setup the DefaultListModel for the JList,
- Properties of the JList to set in Netbeans include:
- model - you can use
the model to preload the list with text
strings (as was done for JComboBox), or setup the model with an
instance of DefaultListModel (select custom code option)
- To setup a DefaultListModel,
- import javax.swing.*;
- Open the properites of the JList box
- press the little square button next to model,
- from the drop down select "Custom Code"
- in the box, fill in new DefaultListModel()
- selectionMode (under
other properties)
- Single - allow the user to select just one thing
- Single-interval - allow the user to select just one
contiguous set
- Multiple-interval - allow the user to select multiple items
in multiple places
- visible row count (under other properties) - set to the
number of items to be seen without scrolling
- Programming:
- to add items, you'll first have to get a reference to the
DefaultListModel, then use the addElement method
String name =
nameField.getText();
String phone =
phoneField.getText();
String email =
emailField.getText();
//
// add to the list box
//
Employee emp = new
Employee(name, phone, email); // create an employee object
DefaultListModel
employeemodel = (DefaultListModel)employeeList.getModel(); // get
a reference to the list model
employeemodel.addElement(emp); // add the employee to the list
- to determine which item is selected, use the getSelectedIndex()
method of the JList. This returns the index of the item selected by the
user,
or -1 if no item is selected.
- to remove items, again you'll have to get a reference to the
DefaultListModel, then use the remove method.
DefaultListModel
employeemodel = (DefaultListModel)employeeList.getModel();
int selected =
employeeList.getSelectedIndex();
//
// remove the selected one
//
if(selected >= 0) // selected
is -1 for nothing selected
employeemodel.remove(selected); //remove one item
- The ListSelectionEvent is triggered
whenever a user selects a
list item.
- To add code to process this event in Netbeans, do the
following:
- right click on the JList (either in the design window or
Inspector window)
- From the popup menu select Events, then ListSelection, then ValueChanged
- Netbeans will then add the method to handle the selection
change event.
- Other userful methods
- JList
- getSelectedIndices() - returns an array of selections
for use in Multiple Select and Single interval select models
- setSelectedIndex and setSelectedIndices - allows your
program to preset the selected items.
- getSelectedValue - returns the first object that is
selected
- getSelectedValues - returns an array of objects that
are selected.
- DefaultListModel
- clear() - removes all elements from the list
- removeAllElements() - same as clear()
- size() - returns the number of items in the list
- add(index, object) - adds an item at a specified index.
- addElement(object) - adds an object to the end of the
list
- get(index) - retrieves the object at the specified
index
- remove(index) - removes the object at the specified
index
- removeElement(object) - removes the object from the
list.
- Note1: If you want to add numbers to a list, you can either add
them as wrapper class objects (Integer and Double), or as a String.
- Note: if you are deleting several items from a JList using the
remove method, then the remaining items in the list are renumbered
after each remove, therefore you should do the removes in reverse
order. E.g. to remove the selected items:
DefaultListModel
employeemodel = (DefaultListModel)employeeList.getModel();
int indices[] = employeeList.getSelectedIndices(); //
get an array of select items
for(int i = indices.length-1; i >= 0; i--) //
remove items one at a time,
employeeModel.remove(indices[i]);
// start with the last one.