< Day Day Up > |
A JList [82] presents the user with a group of items, displayed in one or more columns , to choose from. Lists can have many items, so they are often put in scroll panes.
In addition to lists, the following Swing components present multiple selectable items to the user: combo boxes, menus , tables, and groups of check boxes or radio buttons . To display hierarchical data, use a tree. Figure 30 shows two GUIs that use lists. This section uses these examples as a basis for the discussions that follow. Figure 30. Screenshots of ListDialog (used by ListDialogRunner ) and ListDemo .
Try This:
Initializing a ListHere's the code from ListDialog.java that creates and sets up its list: list = new JList(data); //data has type Object[] list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.setLayoutOrientation(JList.HORIZONTAL_WRAP); list.setVisibleRowCount(-1); ... JScrollPane listScroller = new JScrollPane(list); listScroller.setPreferredSize(new Dimension(250, 80)); The code passes an array to the list's constructor. The array is filled with strings that were passed in from another object. In our example, the strings happen to be boys' names . Other JList constructors let you initialize a list from a Vector or from an object that adheres to the ListModel [84] interface. If you initialize a list with an array or vector, the constructor implicitly creates a default list model. The default list model is immutableyou cannot add, remove, or replace items in the list. To create a list whose items can be changed individually, set the list's model to an instance of a mutable list model class, such as an instance of DefaultListModel . [85] You can set a list's model when you create the list or by calling the setModel method. See Adding Items to and Removing Items from a List (page 270) for an example.
The call to setSelectionMode specifies how many items the user can select and whether they must be contiguous. The next section tells you more about selection modes. The call to setLayoutOrientation lets the list display its data in multiple columns. The value JList.HORIZONTAL_WRAP specifies that the list should display its items from left to right before wrapping to a new row. Another possible value is JList.VERTICAL_WRAP , which specifies that the data be displayed from top to bottom (as usual) before wrapping to a new column. The following figures show these two wrapping possibilities, together with the default, JList.VERTICAL .
In combination with the call to setLayoutOrientation , invoking the method setVisibleRowCount(-1) makes the list display the maximum number of items possible in the available space onscreen. Another common use of setVisibleRowCount is to specify to the lists's scroll pane how many rows the list prefers to display. Selecting Items in a ListA list uses an instance of ListSelectionModel [86] to manage its selection. By default, a list selection model allows any combination of items to be selected at a time. You can specify a different selection mode by calling the setSelectionMode method on the list. For example, both ListDialog and ListDemo set the selection mode to SINGLE_SELECTION (a constant defined by ListSelectionModel ) so that only one item in the list can be selected. The following table describes the three list selection modes.
No matter which selection mode your list uses, the list fires list selection events whenever the selection changes. You can process these events by adding a list selection listener [87] to the list with the addListSelectionListener method. A list selection listener must implement one method: valueChanged . Here's the valueChanged method for the listener in ListDemo :
public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1){ //No selection, disable fire button. fireButton.setEnabled(false); } else { //Selection, enable the fire button. fireButton.setEnabled(true); } } } Many list selection events can be generated from a single user action such as a mouse click. The getValueIsAdjusting method returns true if the user is still manipulating the selection. This particular program is interested only in the final result of the user's action, so the valueChanged method does something only if getValueIsAdjusting returns false . Because the list is in single-selection mode, this code can use getSelectedIndex to get the index of the just-selected item. JList provides other methods for setting or getting the selection when the selection mode allows more than one item to be selected. If you want, you can listen for events on the list's list selection model rather than on the list itself. ListSelectionDemo [88] is an example that shows how to listen for list selection events on the list selection model and lets you change the selection mode of a list dynamically.
Adding Items to and Removing Items from a ListThe ListDemo example that we showed previously features a list whose contents can change. Here's the ListDemo code that creates a mutable list model object, puts the initial items in it, and uses the list model to create a list: listModel = new DefaultListModel(); listModel.addElement("Alison Huml"); listModel.addElement("Kathy Walrath"); listModel.addElement("Lisa Friendly"); listModel.addElement("Mary Campione"); listModel.addElement("Sharon Zakhour"); listModel.addElement("Alan Sommerer"); list = new JList(listModel); This particular program uses an instance of DefaultListModel , a class provided by Swing. In spite of the class name, a list does not have a DefaultListModel unless your program explicitly makes it so. If DefaultListModel doesn't suit your needs, you can write a custom list model, which must adhere to the ListModel interface. The following code snippet shows the actionPerformed method for the action listener registered on the Fire button. The bold line of code removes the selected item in the list. The remaining lines in the method disable the Fire button if the list is now empty, and make another selection if it's not. public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); listModel.remove(index); int size = listModel.getSize(); if (size == 0) { //Nobody's left, disable firing. fireButton.setEnabled(false); } else { //Select an index. if (index == listModel.getSize()) { //removed item in last position index--; } list.setSelectedIndex(index); list.ensureIndexIsVisible(index); } } Here's the actionPerformed method for the action listener shared by the Hire button and the text field: public void actionPerformed(ActionEvent e) { String name = employeeName.getText(); //User didn't type in a unique name... if (name.equals("") alreadyInList(name)) { Toolkit.getDefaultToolkit().beep(); employeeName.requestFocusInWindow(); employeeName.selectAll(); return; } int index = list.getSelectedIndex(); //get selected index if (index == -1) { //no selection, so insert at beginning index = 0; } else { //add after the selected item index++; } listModel.insertElementAt(employeeName.getText(), index); //Reset the text field. employeeName.requestFocusInWindow(); employeeName.setText(""); //Select the new item and make it visible. list.setSelectedIndex(index); list.ensureIndexIsVisible(index); } This bold line of code uses the list model's insertElementAt method to insert the new name after the current selection or, if no selection exists, at the beginning of the list. If you just want to add to the end of the list, you can use DefaultListModel 's addElement method instead. Whenever items are added to, removed from, or modified in a list, the list model fires list data events. Refer to How to Write a List Data Listener (page 682) for information about listening for these events. That section contains an example that is similar to ListDemo but adds buttons that move items up or down in the list. Writing a Custom Cell RendererA list uses an object called a cell renderer to display each of its items. The default cell renderer knows how to display strings and icons. If you want to put any other Object in a list or if you want to change the way the default renderer displays icons or strings, you can implement a custom cell renderer. Take these steps to provide a custom cell renderer for a list:
We don't provide an example of a list with a custom cell renderer, but we do have an example of a combo box with a custom rendererand combo boxes use the same type of renderer as lists. See the example described in Providing a Custom Renderer (page 181). The List APITables 44 through 47 list the commonly used JList constructors and methods. Other methods you are most likely to invoke on a JList object are those such as setPreferredSize that its superclasses provide. See The JComponent API (page 55) for tables of commonly used inherited methods. Also refer to the JList API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JList.html. Much of the operation of a list is managed by other objects. The items in the list are managed by a list model object, the selection is managed by a list selection model object, and most programs put a list in a scroll pane to handle scrolling. For the most part, you don't need to worry about the models because JList creates them as necessary and you interact with them implicitly with JList 's convenience methods. Table 44. Initializing List Data
Table 45. Displaying the List
Table 46. Managing the List's Selection
Table 47. Managing List Data
Examples That Use ListsThis table shows the examples that use JList and where those examples are described.
|
< Day Day Up > |