public class SimpleListModel<T>
extends javax.swing.AbstractListModel
implements java.lang.Iterable<T>
Also very useful is the removeIndices(int[]) method. This is a highly optimized way to remove a set of items from the model firing only the minimum necessary events for performance reasons. It's handy to use this if you have a JList which supports removal of a multiple selection.
The asList()
method returns an object that implements the
List
interface. Changes you make to this list will
automatically fire events to listeners of this list model. This is
convenient if you want to pass a list model down to code that uses the
collections framework.
Constructor and Description |
---|
SimpleListModel()
Create a simple list model based on a new ArrayList.
|
SimpleListModel(java.util.List<T> list)
Create a simple list model based on the specified delegate list.
|
Modifier and Type | Method and Description |
---|---|
void |
addElement(T o)
Add an element, firing an intervalAdded event.
|
void |
addElements(java.util.Iterator<T> i)
Deprecated.
use
asList() .addAll( Collection ). |
java.util.List<T> |
asList()
Returns a
List implementation that is bound to this
list model. |
void |
changed()
Fire a contentsChanged event for the whole list.
|
void |
changed(int index)
Fire a contentsChanged event for the element at the specified index.
|
T |
getElementAt(int i)
Get an element in the model by its index.
|
int |
getSize()
Get the number of elements in the model.
|
int |
indexOf(T item)
Return the position of the item in the list, or -1 if it not in the list.
|
void |
insertAt(int index,
T o)
Insert the specified element at the specified index in the list.
|
java.util.Iterator<T> |
iterator()
Get an iterator over all items in the model.
|
void |
moveBottom(int index)
Move the element at the specified index to the end of the list, firing the
necessary intervalRemoved and intervalAdded events.
|
void |
moveDown(int index)
Move the element at the specified index down one position, firing the
necessary intervalRemoved and intervalAdded events.
|
void |
moveTop(int index)
Move the element at the specified index to the start of the list, firing the
necessary intervalRemoved and intervalAdded events.
|
void |
moveUp(int index)
Move the element at the specified index up one position, firing the
necessary intervalRemoved and intervalAdded events.
|
void |
removeAll()
Remove all elements in the list, firing an intervalRemoved event.
|
void |
removeElement(T o)
Remove a single element, firing an intervalRemoved event.
|
void |
removeIndices(int[] indices)
Remove the specified indices from the model efficiently.
|
public SimpleListModel()
public SimpleListModel(java.util.List<T> list)
list
- the delegate list to base the model on.java.lang.NullPointerException
- if list is null.public java.util.List<T> asList()
List
implementation that is bound to this
list model.The binding behavior means that you can safely call methods on the returned List that make changes to the list, and they will be reflected in the model, firing appropriate events.
public int getSize()
getSize
in interface javax.swing.ListModel
public T getElementAt(int i)
getElementAt
in interface javax.swing.ListModel
i
- the index of an element in the model. Must be >= 0 and
< this.getSize()
.java.lang.IndexOutOfBoundsException
- if the index is out of range (index
< 0 || index >= size()).public java.util.Iterator<T> iterator()
Iterator.remove()
will result in an
UnsupportedOperationException. For a modifiable iterator, use
asList()
.iterator().iterator
in interface java.lang.Iterable<T>
public int indexOf(T item)
item
- @Deprecated public void addElements(java.util.Iterator<T> i)
asList()
.addAll( Collection ).i
- the iterator over elements to add to the list.java.lang.NullPointerException
- if i is null.public void addElement(T o)
o
- the element to add.public void removeIndices(int[] indices)
model.removeIndices( list.getSelectedIndices() );
indices
- the indices to remove. The indices must
be sorted. i.e. for all x : indices[x] < indices[x+1].java.lang.NullPointerException
- if indices is null.public void removeElement(T o)
o
- the element to remove.public void removeAll()
public void changed(int index)
index
- the index of an element that has changed. Must be >=0
and <getSize().java.lang.IndexOutOfBoundsException
- if index is out of bounds.public void changed()
public void moveUp(int index)
index
- the index of an element to move up. Must be >=0
and <getSize(). If index is 0, this method does nothing.java.lang.IndexOutOfBoundsException
- if index is out of bounds.public void moveDown(int index)
index
- the index of an element to move down. Must be >=0
and <getSize(). If index is getSize()-1, this method does
nothing.java.lang.IndexOutOfBoundsException
- if index is out of bounds.public void moveTop(int index)
index
- the index of an element to move to the top. Must be >=0
and <getSize(). If index is 0, this method does nothing.java.lang.IndexOutOfBoundsException
- if index is out of bounds.public void moveBottom(int index)
index
- the index of an element to move to the end. Must be >=0
and <getSize(). If index is getSize()-1, this method does
nothing.java.lang.IndexOutOfBoundsException
- if index is out of bounds.public void insertAt(int index, T o)
index
- the index to insert at.o
- the object to insert.