Class DefaultListModel<T>
- All Implemented Interfaces:
ListModel<T>, MultipleSelectionListModel<T>
- Direct Known Subclasses:
ContactsModel
Default implementation of the list model based on a List of elements.
The list model is an observable set of objects that com.codename1.ui.List uses to pull
the data to display.
public void showForm() {
Form hi = new Form("MultiList", new BorderLayout());
int mm = Display.getInstance().convertToPixels(3);
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(mm * 3, mm * 4, 0), false);
Image icon1 = URLImage.createToStorage(placeholder, "icon1", "http://www.georgerrmartin.com/wp-content/uploads/2013/03/GOTMTI2.jpg");
Image icon2 = URLImage.createToStorage(placeholder, "icon2", "http://www.georgerrmartin.com/wp-content/uploads/2012/08/clashofkings.jpg");
Image icon3 = URLImage.createToStorage(placeholder, "icon3", "http://www.georgerrmartin.com/wp-content/uploads/2013/03/stormswordsMTI.jpg");
Image icon4 = URLImage.createToStorage(placeholder, "icon4", "http://www.georgerrmartin.com/wp-content/uploads/2012/08/feastforcrows.jpg");
Image icon5 = URLImage.createToStorage(placeholder, "icon5", "http://georgerrmartin.com/gallery/art/dragons05.jpg");
ArrayList> data = new ArrayList<>();
data.add(createListEntry("A Game of Thrones", "1996", icon1));
data.add(createListEntry("A Clash Of Kings", "1998", icon2));
data.add(createListEntry("A Storm Of Swords", "2000", icon3));
data.add(createListEntry("A Feast For Crows", "2005", icon4));
data.add(createListEntry("A Dance With Dragons", "2011", icon5));
data.add(createListEntry("The Winds of Winter", "2016 (please, please, please)", placeholder));
data.add(createListEntry("A Dream of Spring", "Ugh", placeholder));
DefaultListModel> model = new DefaultListModel<>(data);
MultiList ml = new MultiList(model);
hi.add(BorderLayout.CENTER, ml);
hi.show();
}
private Map createListEntry(String name, String date, Image icon) {
Map entry = new HashMap<>();
entry.put("Line1", name);
entry.put("Line2", date);
entry.put("icon", icon);
return entry;
}
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of DefaultListModelDefaultListModel(Collection<T> items) Creates a new instance of DefaultListModelDefaultListModel(Vector<T> items) Creates a new instance of DefaultListModelDefaultListModel(T... items) Creates a new instance of DefaultListModel -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked to indicate interest in future change eventsvoidAdds the specified item to the end of this list.voidaddItemAtIndex(T item, int index) Adding an item to list at given indexvoidaddSelectedIndices(int... indices) Adds indices to set of selected indices.voidInvoked to indicate interest in future selection eventsprotected voidfireDataChangedEvent(int status, int index) Broadcast a change event to all listenersgetItemAt(int index) Returns the item at the given offsetgetList()Returns the internal list of items which makes traversal using iterators easier.intReturns the selected list offsetint[]Gets the selected indices in this model.intgetSize()Returns the number of items in the listbooleanChecks to see if this list model is in multi-selection mode.voidRemoves all elements from the modelvoidInvoked to indicate no further interest in future change eventsvoidremoveItem(int index) Removes the item at the specified position in this list.voidremoveSelectedIndices(int... indices) Removes indices from the set of selected indices.voidInvoked to indicate no further interest in future selection eventsvoidChange the item at the given indexvoidsetMultiSelectionMode(boolean multiSelectionMode) Enables or disables multi-selection mode.voidsetSelectedIndex(int index) Sets the selected list offset can be set to -1 to clear selectionvoidsetSelectedIndices(int... indices) For use with multi-selection mode.
-
Constructor Details
-
DefaultListModel
public DefaultListModel()Creates a new instance of DefaultListModel -
DefaultListModel
-
DefaultListModel
Creates a new instance of DefaultListModel
Parameters
items: the items in the model
-
DefaultListModel
Creates a new instance of DefaultListModel
Parameters
items: the items in the model
-
-
Method Details
-
getItemAt
-
getSize
-
getSelectedIndex
public int getSelectedIndex()Returns the selected list offset
Returns
the selected list index
- Specified by:
getSelectedIndexin interfaceListModel<T>
-
setSelectedIndex
public void setSelectedIndex(int index) Sets the selected list offset can be set to -1 to clear selection
Parameters
index: an index into this list
- Specified by:
setSelectedIndexin interfaceListModel<T>
-
addItem
-
setItem
Change the item at the given index
Parameters
-
index: the offset for the item -
item: the value to set
-
-
addItemAtIndex
Adding an item to list at given index
Parameters
-
item: - the item to add -
index: - the index position in the list
-
-
removeItem
public void removeItem(int index) Removes the item at the specified position in this list.
Parameters
index: the index of the item to removed
- Specified by:
removeItemin interfaceListModel<T>
-
removeAll
public void removeAll()Removes all elements from the model -
addDataChangedListener
Invoked to indicate interest in future change events
Parameters
l: a data changed listener
- Specified by:
addDataChangedListenerin interfaceListModel<T>
-
removeDataChangedListener
Invoked to indicate no further interest in future change events
Parameters
l: a data changed listener
- Specified by:
removeDataChangedListenerin interfaceListModel<T>
-
fireDataChangedEvent
protected void fireDataChangedEvent(int status, int index) Broadcast a change event to all listeners
Parameters
-
status: the status of the event -
index: the index changed
-
-
addSelectionListener
Invoked to indicate interest in future selection events
Parameters
l: a selection listener
- Specified by:
addSelectionListenerin interfaceListModel<T>
-
removeSelectionListener
Invoked to indicate no further interest in future selection events
Parameters
l: a selection listener
- Specified by:
removeSelectionListenerin interfaceListModel<T>
-
getList
-
addSelectedIndices
public void addSelectedIndices(int... indices) Adds indices to set of selected indices.
Parameters
indices: Indices to add to selected indices.
- Specified by:
addSelectedIndicesin interfaceMultipleSelectionListModel<T>
-
removeSelectedIndices
public void removeSelectedIndices(int... indices) Removes indices from the set of selected indices.
Parameters
indices: Indices to remove from selected indices.
Since
6.0
- Specified by:
removeSelectedIndicesin interfaceMultipleSelectionListModel<T>
-
getSelectedIndices
public int[] getSelectedIndices()Gets the selected indices in this model. Indices should be returned in increasing order with no duplicates.
Returns
Since
6.0
- Specified by:
getSelectedIndicesin interfaceMultipleSelectionListModel<T>- Returns:
- Selected indices in increasing order with no duplicates. If there are no selected indices, then this will return a zero-length array.
-
setSelectedIndices
public void setSelectedIndices(int... indices) For use with multi-selection mode. Sets the selected indices in this model.
Note: This may fire multiple selectionChange events. For each "deselected" index, it will fire an event with the (oldIndex, newIndex) being (index, -1) (i.e. selected index changes from the index to -1. And for each newly selected index, it will fire the event with (oldIndex, newIndex) being (-1, index).
Parameters
indices: The indices to select.
Throws
IllegalArgumentException: If#isMultiSelectionMode()is false, and indices length is greater than 1.
Since
6.0
See also
-
#setMultiSelectionMode(boolean)
-
#isMultiSelectionMode()
- Specified by:
setSelectedIndicesin interfaceMultipleSelectionListModel<T>
-
isMultiSelectionMode
public boolean isMultiSelectionMode()Checks to see if this list model is in multi-selection mode.
Returns
the multiSelectionMode
Since
6.0
-
setMultiSelectionMode
public void setMultiSelectionMode(boolean multiSelectionMode) Enables or disables multi-selection mode.
Parameters
multiSelectionMode: the multiSelectionMode to set
Since
6.0
-