Hi all. Excuse me because I am quite new to swing. What I want to do is this: I have these objects "contact", they store info like name, number , email etc, it has getters and setters for these properties. I then have a vector which holds a number of these contact objects.
On my panel I have a Jlist in which I would like to display the name and next to them a couple of text fields which display the number, email etc for the name in the JList. The idea is you select the name in the list and the text fields are updated to show the corrisponding number, email etc.
If I set the list up with the vector of contacts then obviously you see the list of String representations of each contact object.
My question is .. how can I set up the list to show just the name from each contact, but still know which object has been selected? Should I make a seperate String array of names, then when I select them use the index number of the selected name to select the details for the text fields from the contacts vector? ... this seems a bit cumbersome and I am sure that there must be a neater way...
If I havnt been clear let me know and I will try to explain a little better... thanks in advance of any help..
Alex
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
My question is .. how can I set up the list to show just the name from each contact, but still know which object has been selected?
i dont remember quite well, but some time ago i had this list and, on getSelectedValue() (or smilar action) i retrieved the object, and then could access their accessors...
hope this helps
java amateur
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
i remember now
Scott Delap
author
Ranch Hand
Joined: Apr 05, 2005
Posts: 73
posted
0
Alex,
You can implement a custom renderer that will just display the name of the object. I would keep the list itself containing full object references. Here is Sun's JList tutoral. There is a link at the bottom of the article about custom renderers.
You can then add a listener for selection change. On each change get the selected object and populate your values. If you'd like to look at an even easier way to do things, checkout JGoodies Data Binding.
The concept is data binding is a bit more advanced than basic Swing. However once you get the hang of it, you can create forms quite quickly that are wired to your domain objects.
i wonder if there's anything related with Hibernate too
i also wonder why you'r not quoted (here at javaranch) as an Author
anyway binding looks interesting: in my case i just needed to alter some boolean fied and my way looked simple enough...
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
here's a simple example
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
posted
0
that's it!!
Alex Joyce
Greenhorn
Joined: Feb 17, 2005
Posts: 13
posted
0
Thanks everyone for your help... I really appreciate the time you took to help me with a solution. I believe I am just about there with it now. I must also say that this is such an invaluable resource...