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:

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.