Typed DefaultListModel to avoid casting

Is there a way in java to have a ListModel that only accepts a certain type? What I'm looking for is something like DefaultListModel<String> or TypedListModel<String>, because the DefaultListModel only implements addElement(Object obj) and get(int index) which returns Object of course.

That way I always have to cast from Object to e.g. String and there is no guarantee that there are only strings in my model, even though I'd like to enforce that.

Is this a flaw or am I using list models the wrong way?


The class DefaultListModel does have this up the top in the source:

This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support, and notifies the ListDataListeners when changes occur. Presently it delegates to a Vector, in a future release it will be a real Collection implementation.

Sounds like it's just an old class. I guess you could write your own version of it (implement the AbstractListModel interface) if you are desperate enough. If you are just annoyed about having to cast it to a String all the time surely the foreach syntax will do that for you?

Need Your Help

jQuery Autocomplete - Prevent submit of anything that is not a value from the autocomplete array?

javascript jquery autocomplete

I am trying to create a "jump to page" type of search on a website using a jQuery autocomplete search form.

Java LDAP with SASL

java ldap sasl

I am using Java 1.2 with the Netscape ldapjdk.jar and trying to make an LDAP connection to one of our servers that needs SASL. I’m also relativity new to LDAP. I do know that java EE has built in...