This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes I need help creating a custom ListModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "I need help creating a custom ListModel " Watch "I need help creating a custom ListModel " New topic
Author

I need help creating a custom ListModel

Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Suppose I have a class like:

Now I have a vector of those foo objects and I want to display string (a) in a JList and have it be able to update the JList when a new foo object is added to the vector. I need to create a custom ListModel to do this but I cannot figure out how to get it to update the JList when new objects are added to the vector.
All the examples I have found on the net so far are very trivial and only use DefaultListModel but I have a more complex data model then just a vector of strings like DefaultListModel handles easily.
Can somebody help?
Thanks,
Frank
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
A class implementing ListModel directly needs to keep track of any registered listeners and notify them of any changes. This is kind of a pain to write, and AbstractListModel does much of the work for you, so I suggest using it. You just have to add any desired methods to change the list contents, and make sure you call the appropriate fireXXX() method afterward to tell the AbstractListModel to notify listeners of the change just made. Here's a simple example:
<code><pre>class FooListModel extends AbstractListModel {

private Vector fooVector = new Vector();

// required to implement ListModel:
public Object getElementAt(int index) {
return ((String) fooVector.elementAt(index)).a;
}

// required to implement ListModel:
public int getSize() {
return fooVector.size();
}

// sample additional method allowing modification of list:
public void addFooAt(int index, foo newFoo) {
fooVector.add(index, newFoo);
fireIntervalAdded(this, index, index);
}

// add more methods as you wish
}
</pre></code>
Note that the Vector is contained within the FooListModel, and only accessible through the model's methods. This way we can guarantee that any time the Vector is modified, we can make sure we call the correct fireXXX() method to go with it. Incidentally I'd probably use an ArrayList rather than a Vector, but kept the Vector here for familiarity.


"I'm not back." - Bill Harding, Twister
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
I can't seem to get your code to work. I'm using a vector to store my data. However I am not adding my data like you have below because there is no way to know at what index I want to add the data too. At least I am not sure how to determine that. I use addElement and the data goes wherever it goes in the vector. I call the fireIntervalAdded but I am not sure what goes int the index fields. I have looked at the docs a million times over and cannot for the life of me figure out what they mean by there definition of interval.
-------
fireContentsChanged
protected void fireContentsChanged(Object source,
int index0,
int index1)
AbstractListModel subclasses must call this method after one or more elements of the list change. The changed elements are specified by a closed interval index0, index1, i.e. the range that includes both index0 and index1. Note that index0 need not be less than or equal to index1.
Parameters:
source - The ListModel that changed, typically "this".
index0 - One end of the new interval.
index1 - The other end of the new interval.
See Also:
EventListenerList, DefaultListModel
---------

[This message has been edited by Frank Hale (edited October 21, 2000).]
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
index0 - One end of the new interval.
index1 - The other end of the new interval.
What does this mean?
Anyway I figured it out but I would like to know what they mean by interval index0 and index1.
Thanks,
Frank

[This message has been edited by Frank Hale (edited October 22, 2000).]
 
Don't get me started about those stupid light bulbs.
 
subject: I need help creating a custom ListModel
 
Similar Threads
horizontal JList
JList : how to add one simple element to a existing list??
How to update JList
Serializing/deserializing a list
How can I update the a JList?