Java Reference
In-Depth Information
jb = new JButton("removeRange FM");
jp2.add(jb);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int size = model.getSize();
if (size != 0)
model.removeRange(0,size/2);
}
});
frame.add(jp, BorderLayout.SOUTH);
frame.setSize(640, 300);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 13-2 shows the output for one such run, after several buttons were selected.
Figure 13-2.
A listing for data model changes
■
Note
To help you decode the button labels in Figure 13-2, an
F
means the method affects the first cell, an
M
means it affects the middle cell, and an
L
means it affects the last cell. The removeElement “Last” button
will remove the first element in the data model whose content is
Last
.
The retrieving methods of the
DefaultListModel
class are quite varied in their capabilities.
The class has the basic accessor methods
public Object get(int index)
,
public Object
getElementAt(int index)
, and
public Object elementAt(int index)
, which all do the same
thing. The
DefaultListModel
class also has more specific methods. For instance, to work with
all elements, you can obtain an instance of
Enumeration
using
public Enumeration elements()
.