We help IT Professionals succeed at work.

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

Suppai
Suppai asked
on
Medium Priority
873 Views
Last Modified: 2013-11-23
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.
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
use toArray()

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

ksivananthVice President

Commented:
no and even you can't convert them!
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sorry that should be:

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

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
or if you just want a List

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

Author

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?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
does not matter what type if objects are in the list

ksivananthVice President
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!

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
ksivananthVice President

Commented:
if a Vector parameterezied with String, it can't take Object!
CERTIFIED EXPERT
Top Expert 2016
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

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
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.

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

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

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
With a utility method you'd get the added advantage that it would work with *any* ListModel.
CERTIFIED EXPERT
Top Expert 2016

Commented:
So if you wanted a Vector specifically for some reason, you could just do
Vector v = new Vector(collectionsNiceListModel);

Open in new window

Author

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:-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.