Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set up Jlist

 
Alex Joyce
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i remember now

 
Scott Delap
author
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a simple example
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's it!!
 
Alex Joyce
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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





 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic