Introduction to Java Programming-Comprehensive Version (6th Edition)
30.8. List Models
The preceding example constructs a list with a fixed set of strings. If you want to add new items to the list or delete existing items, you have to use a list model. This section introduces list models.
The JList class delegates the responsibilities of storing and maintaining data to its data model. The JList class itself does not have methods for adding or removing items from the list. These methods are supported in ListModel , as shown in Figure 30.20.
Figure 30.20. ListModel stores and manages data in a list.
(This item is displayed on page 1030 in the print version)
All list models implement the ListModel interface, which defines the registration methods for ListDataEvent . The instances of ListDataListener are notified when the items in the list are modified. ListModel also defines the methods getSize and getElementAt . The getSize method returns the length of the list, and the getElementAt method returns the element at the specified index.
AbstractListModel implements the ListModel and Serializable interfaces. AbstractListModel implements the registration methods in ListModel , but does not implement the getSize and getElementAt methods.
DefaultListModel extends AbstractListModel and implements the two methods getSize and getElementAt , which are not implemented by AbstractListModel .
The methods in DefaultListModel are similar to those in the java.util.Vector class. You use the add method to insert an element to the list, the remove method to remove an element from the list, the clear method to clear the list, the getSize method to return the number of elements in the list, and the getElementAt method to retrieve an element. In fact, the DefaultListModel stores data in an instance of Vector , which is essentially a resizable array. Swing components were developed before the Java Collections Framework. In future implementations , Vector may be replaced by java.util.ArrayList .
Note
| In most cases, if you create a Swing GUI object without specifying a model, an instance of the default model class is created. But this is not true for JList . By default, the model property in JList is not an instance of DefaultListModel . To use a list model, you should explicitly create one using DefaultListModel . |
Listing 30.8 gives an example that creates a list using a list model and allows the user to add and delete items in the list, as shown in Figure 30.21. When the user clicks the Add new item button, an input dialog box is displayed to receive a new item.
Figure 30.21. You can add elements and remove elements in a list using list models.
Listing 30.8. ListModelDemo.java
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class ListModelDemo extends JApplet { 6 private DefaultListModel listModel = new DefaultListModel(); 7 private JList jlst = new JList(listModel); 8 private JButton jbtAdd = new JButton( "Add new item" ); 9 private JButton jbtRemove = new JButton( "Remove selected item" ); 10 11 /** Construct the applet */ 12 public ListModelDemo() { 13 // Add items to the list model 14 listModel.addElement( "Item1" ); 15 listModel.addElement( "Item2" ); 16 listModel.addElement( "Item3" ); 17 listModel.addElement( "Item4" ); 18 listModel.addElement( "Item5" ); 19 listModel.addElement( "Item6" ); 20 21 JPanel panel = new JPanel(); 22 panel.add(jbtAdd); 23 panel.add(jbtRemove); 24 25 add(panel, BorderLayout.NORTH); 26 add( new JScrollPane(jlst), BorderLayout.CENTER); 27 28 // Register listeners 29 jbtAdd.addActionListener( new ActionListener() { 30 public void actionPerformed(ActionEvent e) { 31 String newItem = 32 JOptionPane.showInputDialog( "Enter a new item" ); 33 34 if (newItem != null ) 35 if (jlst.getSelectedIndex() == - 1 ) 36 listModel.addElement(newItem); 37 else 38 listModel.add(jlst.getSelectedIndex(), newItem); 39 } 40 }); 41 42 jbtRemove.addActionListener( new ActionListener() { 43 public void actionPerformed(ActionEvent e) { 44 listModel.remove(jlst.getSelectedIndex()); 45 } 46 }); 47 } 48 }
|
The program creates listModel (line 6), which is an instance of DefaultListModel , and uses it to manipulate data in the list. The model enables you to add and remove items in the list.
A list is created from the list model (line 7). The initial elements are added into the model using the addElement method (lines 14 “19).
To add an element, the user clicks the Add new item button to display an input dialog box. Type a new item in the dialog box. The new item is inserted before the currently selected element in the list (line 38). If no element is selected, the new element is appended to the list (line 36).
To remove an element, the user has to select the element and then click the Remove selected item button. Note that only the first selected item is removed. You can modify the program to remove all the selected items (see Exercise 30.6).
What would happen if you click the Remove selected item button but no items are currently selected? This would cause an error. To fix it, see Exercise 30.6.