?
Solved

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

Posted on 2009-02-18
17
Medium Priority
?
752 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.
0
Comment
Question by:Suppai
  • 8
  • 3
  • 3
  • +1
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 23668509
use toArray()

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

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 23668518
no and even you can't convert them!
0
 
LVL 92

Expert Comment

by:objects
ID: 23668521
sorry that should be:

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 23668528
or if you just want a List

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

0
 

Author Comment

by:Suppai
ID: 23668538
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
 
LVL 92

Expert Comment

by:objects
ID: 23668547
does not matter what type if objects are in the list

0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 75 total points
ID: 23668556
>>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
 

Author Comment

by:Suppai
ID: 23668560
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 23668565
if a Vector parameterezied with String, it can't take Object!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 23668572
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
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 23668586
> 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
 
LVL 92

Expert Comment

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

0
 
LVL 92

Expert Comment

by:objects
ID: 23668600
With a utility method you'd get the added advantage that it would work with *any* ListModel.
0
 
LVL 86

Expert Comment

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

Open in new window

0
 

Author Closing Comment

by:Suppai
ID: 31548180
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 23670823
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 23675934
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question