I'm trying to get a JList to list the elements of an ArrayList, so that if a user selects an element from the JList, a method will be run which changes properties of the element in the original ArrayList. I can't get the JList to accept the ArrayList as a parament. I thought the JList construtor:
JList(Object[] listData)
Constructs a JList that displays the elements in the specified array.
would take it but it's not working.
Here is a simple example of what I'm trying to do:
here's where I make the ArrayList containing two objects
ArrayList cards = new ArrayList();
Card one = new Card(1);
Card two = new Card(2);
cards.add(one);
cards.add(two);
Here's where I try to make the JList, though I know it doesn't work. I get an error saying it cannot find a constructor for JList that takes an ArrayList (I thought maybe it would treat it as an Object array, but clearly it doesn't)
JList list = new javax.swing.JList(cards);
here is the Card class
public class Card{
int value;
public Card(int n){
this.value = n;
}
I want to be able to have the user select an item from the list (say "one") and then have list.getSelectedValue return a reference to "one" in the ArrayList "cards." That way any change made to it (such as: one.value++) will happen in the original ArrayList.
I orginally had all this set up with DefaultListModels for all my different JLists, but then the object returned by list.getSelectedValue was not my original object.
So questions: Can I set up a JList which references the original objects in an ArrayList? If not, how else can I do what I'm trying to accomplish? Should I use a vector instead? ( Don't know much about Vectors, the tutorials I've read don't say much about them. Do they resize automaticaly like ArrayLists?)
Thanks in advance for your help!
Matt