Storing objects in a Jlist
Hi.
I have a form where i can enter values and add them to a Room object I have created. Now what I want is to have a JList populated by these Room objects so that when I select one item in the JList the data stored in that object is put in the form for editing. ('Im using NetBeans)
My problem is that I can only add Strings to the JList, and cant really se how to "link" these strings to my actual objects.
My list format looks like this "[roomnumber] Roomname", but this is just string parts I pull from the object, and don't really "connect" the item to anything.
Does anyone have any pointers to get me in the right direction? As Im not very experienced with java it might be that JList is not the way to achieve my goal?
Parashurama
Re: Storing objects in a Jlist
Quote:
Originally Posted by
Parashurama
My problem is that I can only add Strings to the JList
And why is that?
db
Re: Storing objects in a Jlist
Excellent answer! Thanks for making me think.
I just asumed as much since the examples I looked at just used strings. My bad...
Ok, so I added the Room item directly, but I still struggle to have them show up the way I want. They just appear like this in the list : "Editors.Room@d5e270" while I want it like I mentioned in the first post.
Do I set the "visible name" in the Jlist in the same addElement method?
Im using DefaultListModel, but none of the methods in the link below seems to do what I need:
DefaultListModel (Java Platform SE 7 )
Got another pointer for me? :)
Parashurama
Re: Storing objects in a Jlist
Either override toString() or use a custom renderer.
db
Re: Storing objects in a Jlist
Ok, Ill look into the ListCellRenderer, thanks!
Re: Storing objects in a Jlist
Now I got the List to look the way I wanted, I just passed the string parts I wanted to my CellRendered class and added the string to a textfield which was placed in the list.
Thanks again for your help DarryBruke!