Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anthony Gilke
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Anthony Gilke
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Those aren't the memory addresses.
2. I've already told you what JList uses as the display value.
 
Anthony Gilke
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic