aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Set up Jlist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Set up Jlist" Watch "Set up Jlist" New topic
Author

Set up Jlist

Alex Joyce
Greenhorn

Joined: Feb 17, 2005
Posts: 13

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
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
i remember now

Scott Delap
author
Ranch Hand

Joined: Apr 05, 2005
Posts: 73
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.

http://java.sun.com/docs/books/tutorial/uiswing/components/list.html

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.

https://binding.dev.java.net

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.

Scott
ClientJava.com
Desktop Java Live
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
@Scott

your post took me to read this article of yours.

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
here's a simple example
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
that's it!!
Alex Joyce
Greenhorn

Joined: Feb 17, 2005
Posts: 13
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...

Thanks again.

Alex





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set up Jlist
 
Similar Threads
Project Help
Project Help
Vectors and Collections and Hashtables - which to choose
Model design confusion...
Serching for a surname in my collection