This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Adding array list to JList - MVC - Getting nothing but memory addresses! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding array list to JList - MVC - Getting nothing but memory addresses!" Watch "Adding array list to JList - MVC - Getting nothing but memory addresses!" New topic
Author

Adding array list to JList - MVC - Getting nothing but memory addresses!

Anthony Gilke
Greenhorn

Joined: Mar 01, 2012
Posts: 8
I have an array list in a separate class called "Bottle" and a "View" class with a method called InitView. I have a contentpane that displays a JList and it populates the bottles memory addresses, but i need it to to populate the string version so it's readable...

Any help would be greatly appreciated.



VIEW CLASS



BOTTLE CLASS
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

By default, JList uses the toString() method of the objects populating it to obtain a String for display. You have two options: override Object's toString() in your Bottle class, or use a custom ListCellRenderer, probably extending DefaultListCellRenderer, that assembles the String you would like to display and sets it as the text of the renderer. For the latter approach, go through the Oracle tutorial on How to Use Lists which is linked from the API for JList.

edit On a closer look at your code, you're adding the values returned by toString() to a DefaultListModel<String>. You might want to consider using a DefaultListModel<Bottle> instead, as that can give you direct access to the selected Bottle via getSelectedItem().


luck, db
There are no new questions, but there may be new answers.
Anthony Gilke
Greenhorn

Joined: Mar 01, 2012
Posts: 8
Darryl Burke wrote:By default, JList uses the toString() method of the objects populating it to obtain a String for display. You have two options: override Object's toString() in your Bottle class, or use a custom ListCellRenderer, probably extending DefaultListCellRenderer, that assembles the String you would like to display and sets it as the text of the renderer. For the latter approach, go through the Oracle tutorial on How to Use Lists which is linked from the API for JList.

edit On a closer look at your code, you're adding the values returned by toString() to a DefaultListModel<String>. You might want to consider using a DefaultListModel<Bottle> instead, as that can give you direct access to the selected Bottle via getSelectedItem().


I've added this but still get the memory addresses!


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

1. Those aren't the memory addresses.
2. I've already told you what JList uses as the display value.
Anthony Gilke
Greenhorn

Joined: Mar 01, 2012
Posts: 8
Darryl Burke wrote:1. Those aren't the memory addresses.
2. I've already told you what JList uses as the display value.



Sorry I'm not sure of the proper term but it's not in a readable format. Just gives me bottlePackage.Bottle@15663a2 instead of the actual bottle data.

I think you told me to use getSelectedItem() but with the listModel I can't seem to pull that up.

I'll keep trying but appreciate any assistance. Thanks.
 
wood burning stoves
 
subject: Adding array list to JList - MVC - Getting nothing but memory addresses!
 
Similar Threads
urgent: retrieval of data from database to JList
Compare Function
Won't repaint!
JList & Observer
Prevent Duplicate value from jlist to jlist by way of a jbutton