• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

I need help creating a custom ListModel

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic