DefaultListModel Class

Package: javax.swing

DefaultListModel provides a simple implementation of a list model, which can be used to manage items displayed by a JList control.

CrossRef.eps For more information, see JList Class.

Constructor

Constructor

Description

DefaultListModel()

Creates a new list model object

Methods

Method

Description

void add(Object element, int index)

Adds an element at the specified position

void addElement(Object element))

Adds an element to the end of the list

void clear()

Removes all elements from the list

boolean Contains(Object element)

Returns true if the specified element is in the list

Object firstElement()

Returns the first element in the list

Object get(int index)

Returns the element at the specified location

boolean isEmpty()

Returns true if the list is empty

Object lastElement()

Returns the last element in the list

void remove(int index)

Removes the element from the specified position in the list

void removeElement (Object element)

Removes the specified element from the list

int size()

Returns the number of elements in the list

Object[] toArray()

Returns an array containing each element in the list

When you create the default data model, it’s empty, but you can call the add or addElement method to add elements to the list, as in this example:

String[] values =

{“Pepperoni”, “Sausage”, “Linguica”,

“Canadian Bacon”, “Salami”, “Tuna”,

“Olives”, “Mushrooms”, “Tomatoes”,

“Pineapple”, “Kiwi”, “Gummy Worms”};

DefaultListModel model = new DefaultListModel();

for (String value : values)

model.addElement(value);

Here, the elements from the values array are added to the list model. When you create the JList control, pass the list model to the JList constructor, like so:

list = new JList(model);

You can remove an element from the list model by calling the remove or removeElement method. To remove all the elements from the model, call the clear method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset