Does a Java Conversion method from ListModel (DefaultListModel) to Vector exist?

I was wondering if a methods as mentioned in subject exist in the Java API? It is not that I can't create a loop that does the trick pretty easily, but I like to use the Java API as much as possible.
SuppaiAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
ListModel is horribly broken for collections. See how you get on with this:
package net.proteanit.util;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
 
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
 
public class CollectionsNiceListModel<T> extends AbstractListModel implements
		List, ComboBoxModel {
	private List<T> delegate = new ArrayList<T>();
 
	private T selectedObject;
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
 
	public CollectionsNiceListModel() {
		super();
	}
 
	public CollectionsNiceListModel(Collection<T> items) {
		delegate.addAll(items);
		fireIntervalAdded(this, 0, delegate.size() - 1);
	}
 
	public CollectionsNiceListModel(T[] items) {
		delegate.addAll(Arrays.asList(items));
		fireIntervalAdded(this, 0, delegate.size() - 1);
	}
 
	public Object getElementAt(int index) {
		return delegate.get(index);
	}
 
	public T get(int index) {
		return delegate.get(index);
	}
 
	public int getSize() {
		return delegate.size();
	}
 
	@SuppressWarnings("unchecked")
	public void addElement(Object obj) {
		int index = delegate.size();
		delegate.add((T) obj);
		fireIntervalAdded(this, index, index);
	}
 
	/*
	 * public void add(T obj) { int index = delegate.size(); delegate.add(obj);
	 * fireIntervalAdded(this, index, index); }
	 */
	public void removeElementAt(int index) {
		delegate.remove(index);
		fireIntervalRemoved(this, index, index);
	}
 
	@SuppressWarnings("unchecked")
	public void add(int index, Object o) {
		delegate.add(index, (T) o);
		fireIntervalAdded(this, index, index);
	}
 
	@SuppressWarnings("unchecked")
	public boolean add(Object o) {
		int index = delegate.size();
		delegate.add((T) o);
		fireIntervalAdded(this, index, index);
		return true;
	}
 
	@SuppressWarnings("unchecked")
	public boolean addAll(Collection c) {
		int originalSize = delegate.size();
		delegate.addAll(c);
		this.fireIntervalAdded(this, originalSize, c.size());
		return true;
	}
 
	public boolean addAll(int index, Collection c) {
		int origIndex = index;
		for (Object o : c) {
			this.add(index++, o);
		}
		this.fireIntervalAdded(this, origIndex, c.size());
		return true;
	}
 
	public void clear() {
		int size = delegate.size();
		if (size > 0) {
			delegate.clear();
			this.fireIntervalRemoved(this, 0, size - 1);
		}
	}
 
	public boolean contains(Object o) {
		return delegate.contains(o);
	}
 
	public boolean containsAll(Collection c) {
		boolean result = true;
		for (Iterator iter = c.iterator(); result && iter.hasNext();) {
			result = delegate.contains(iter.next());
		}
		return result;
	}
 
	public int indexOf(Object o) {
		return delegate.indexOf(o);
	}
 
	public boolean isEmpty() {
		return delegate.isEmpty();
	}
 
	public Iterator iterator() {
		return delegate.iterator();
	}
 
	public int lastIndexOf(Object o) {
		return delegate.lastIndexOf(o);
	}
 
	public ListIterator listIterator() {
		return delegate.listIterator();
	}
 
	public ListIterator listIterator(int index) {
		return delegate.listIterator(index);
	}
 
	public boolean remove(Object o) {
		int index = delegate.indexOf(o);
		fireIntervalRemoved(this, index, index);
		return delegate.remove(o);
	}
 
	public Object remove(int index) {
		fireIntervalRemoved(this, index, index);
		return delegate.remove(index);
	}
 
	public boolean removeAll(Collection c) {
		boolean result = false;
		for (Iterator iter = c.iterator(); iter.hasNext();) {
			Object o = iter.next();
			int elementIndex = delegate.indexOf(o);
			if (elementIndex > -1) {
				boolean removed = delegate.remove(o);
				if (removed) {
					result = true;
					this.fireIntervalRemoved(this, elementIndex, elementIndex);
				}
			}
		}
		return result;
	}
 
	public boolean retainAll(Collection c) {
		/**
		 * See if 'c' contains each of our delegate's elements. If it's not
		 * there, remove it
		 */
		boolean result = false;
		for (ListIterator iter = delegate.listIterator(); iter.hasNext();) {
			Object o = iter.next();
			if (!c.contains(o)) {
				System.out.printf("Removing item %s\n", o);
				result = true;
				iter.remove();
				int ix = iter.nextIndex();
				this.fireIntervalRemoved(this, ix, ix);
			}
		}
		return result;
	}
 
	@SuppressWarnings("unchecked")
	public Object set(int index, Object o) {
		fireContentsChanged(this, index, index);
		return delegate.set(index, (T) o);
	}
 
	public int size() {
		return delegate.size();
	}
 
	public List subList(int fromIndex, int toIndex) {
		return delegate.subList(fromIndex, toIndex);
	}
 
	public Object[] toArray() {
		return delegate.toArray();
	}
 
	@SuppressWarnings("unchecked")
	public Object[] toArray(Object[] a) {
		return delegate.toArray(a);
	}
 
	public Object getSelectedItem() {
		return selectedObject;
	}
 
	@SuppressWarnings("unchecked")
	public void setSelectedItem(Object o) {
		if ((selectedObject != null && !selectedObject.equals(o))
				|| selectedObject == null && o != null) {
			selectedObject = (T) o;
			fireContentsChanged(this, -1, -1);
		}
	}
 
	/**
	 * Convenience method to set the first item selected
	 * 
	 */
 
	public void setFirstSelected() {
		if (delegate.size() > 0) {
			setSelectedItem(delegate.get(0));
		}
	}
 
}

Open in new window

0
 
objectsCommented:
use toArray()

Vector v = new Vector(listmodel.toArray());

0
 
ksivananthCommented:
no and even you can't convert them!
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
sorry that should be:

Vector v = new Vector(Arrays.asList(listmodel.toArray()));

0
 
objectsCommented:
or if you just want a List

List list = Arrays.asList(listmodel.toArray());

0
 
SuppaiAuthor Commented:
I actually found out about the v.copyInto(dlm.toArray()), which is mainly the same concept. However if supposedly the dlm contains objects that are not Strings, what will happen? Is the toString method just used or?
0
 
objectsCommented:
does not matter what type if objects are in the list

0
 
ksivananthConnect With a Mentor Commented:
>>Is the toString method just used or?

the vector can hold Strings and Objects as well! The toString is used implicitly where an object to be converted as String!
0
 
SuppaiAuthor Commented:
Ahh I didnt make myself clear I want it to be converted to a Vector<String> and then it seems to matter, at least my editor thinks so. I want to make sure that the vector contains only Strings in order to avoid exceptions later on in the application.
0
 
ksivananthCommented:
if a Vector parameterezied with String, it can't take Object!
0
 
objectsConnect With a Mentor Commented:
> Ahh I didnt make myself clear I want it to be converted to a Vector and then it seems to matter, at least my editor thinks so. I want to make sure that the vector contains only Strings in order to avoid exceptions later on in the application.

you'll need use a utility method for that.

0
 
objectsCommented:
Easy enough write though, would just be a simple loop.
Let me know if you need a hand :)

0
 
objectsCommented:
With a utility method you'd get the added advantage that it would work with *any* ListModel.
0
 
CEHJCommented:
So if you wanted a Vector specifically for some reason, you could just do
Vector v = new Vector(collectionsNiceListModel);

Open in new window

0
 
SuppaiAuthor Commented:
Nice class, but maybe a little overkill for my school project:-) Yes an utility function would be good, I guess it is just a matter if identifying the class of the elements one by one in the loop and taking action if theyre not strings. However ended up realizing that i had taken care of the String checking otherwhere in the application so thorough handling of non-Strings was never necessary...but of course it is always good code to handle everything to increase re-usability I know:-)
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
Using a custom list model is unnecessary and just adds another (messy) class to maintain.
Not to mention that all that code does is what the simple loop I described would do just with a lot less code. It doesn't do anything that the existing model classes don't already do.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.