• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving object reference from jList  RSS feed

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine i've a class Person:


Now (at runtime) i can create n persons:
Person A = new Person();
Person B = new Person();
Person C = new Person();
...
and add them to a Set.
I also add their name to a jList's ListModel.

Now, i add the first two names to another jList's ListModel and simultaneously add them to another Set too.

What i've now:
left jList right jList
A A
B B
C
...

What i'm looking for is a way to retrieve the object's reference who's name is the one we click in right side jList, in order to be able to call add/remove methods on apropriated reference:
(A or B or whatever) A.addStuff();

Hope i was clear enough...
Thanks in advance
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you can do is have this in the Person class
public String toString(){return name;}

Then, you add the whole person object to the list. Because it has a toString it will show the name (because that is what you have defined the toString as) and then you have a reference to the object right there.

Cheers
Rachel
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hope this is what you want.

Nikhil
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for your idea


i did it like this:

and worked fine
thanks again
[ November 16, 2004: Message edited by: miguel lisboa ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!