| 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: 4163
|
|
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: 4163
|
|
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.
|
 |
 |
|
|
subject: Adding array list to JList - MVC - Getting nothing but memory addresses!
|
|
|