Java Reference
In-Depth Information
user can implement the interface
ListModel
for this purpose. However, there is
a predefined implementation of list models in class
DefaultListModel
which is
sufficient for many purposes. In a list with
n
entries, the list entries are numbered
0
,
,...,
−
1, where 0 denotes the first (topmost) entry. When a list element is
added or removed then the numbering is updated. In the following we describe
class
DefaultListModel
which is located in the
javax.swing
library.
1
n
DefaultListModel()
int
size()
boolean
contains(Object entry)
Object get(
int
position)
void
insertElementAt(Object entry,
int
position)
void
addElement(Object entry)
void
removeElementAt(
int
position)
boolean
removeElement(Object entry)
void
removeAllElements()
DefaultListModel()
is the constructor which creates a list model without any
entries.
size()
returns the number of entries currently in the list model.
contains(Object entry)
returns
true
if object
entry
is in the list model, and
false
otherwise.
get(int position)
returns the list entry at position
position
. Throws an ex-
ception if
position
is negative or greater than or equal to the current number
of list entries. As the return type is
Object
,anexplicit cast to the correct type
might be needed.
insertElementAt(Object entry, int position)
adds entry
entry
at position
position
to the list model. Throws an exception if
position
is an invalid
index.
addElement(Object entry)
adds an
entry
at the end of the list.
removeElementAt(int position)
removes the entry at position
position
.
Throws an exception if
position
is an invalid index. The remaining list entries
are renumbered to maintain a consecutive indexing.
removeElement(Object entry)
removes the entry
entry
.Ifsuccessful
true
is
returned, otherwise
false
is returned.
removeAllElements()
removes all entries from the list model.
Once the list model is defined, one passes it to a
JList
in the constructor or by
using the
set
-method:
JList(ListModel lModel)
setModel(ListModel lModel)
Search WWH ::
Custom Search