aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes not able to get the items from JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "not able to get the items from JList" Watch "not able to get the items from JList" New topic
Author

not able to get the items from JList

abhijeet shrivastava
Greenhorn

Joined: Jan 05, 2007
Posts: 10
Hi All,
I am using JList and added it to scrollpane and i am retrieving the selected items from the list with the help of the methyod getSelectedValues(),which returns an array of objects.
now problem is this returning array have the value like this -

Ljava.lang.Object;@198dfaf

please help me out i had tried toString()method also didnt work .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
But your method has worked. What you presumably have in your List is a number of Objects. What you have done is to invoke the toString() method of the java.lang.Object class.

JList and ListModel don't appear to be generic classes so you can't say nor wouldwork.

Try the following, and see whether it works:

Cast each object you get out of the getSelectedValues() array to the class it is supposed to be.

NB: Don't try casting the whole array; I don't think you can cast an Object[] array to a String[] array.
Make sure you only put instance of one class into the JList, otherwise you get a ClassCastException when you try to retrieve them.

CR
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Campbell Ritchie:
What you have done is to invoke the toString() method of the java.lang.Object class.


In fact he invoked it *on the array*.

In contrast to the Collection implementations, an array won't show you it's content when you call toString on it.

Cast each object you get out of the getSelectedValues() array to the class it is supposed to be.


Casting won't help here, because toString is polymorphic, anyway.

Calling toString on each single object instead of the array, for example in a for-loop, that will help.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
abhijeet shrivastava
Greenhorn

Joined: Jan 05, 2007
Posts: 10
Hi,
ya case was that i was invoking toString method on array.when i invoked it at indivisual elements i got the right result.

Thanks ranchars !!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
Only too pleased to help, and thank you, Ilja Preuss.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not able to get the items from JList
 
Similar Threads
How do I get a string from an item in a dynamic jlist?
JList maximum size?
BorderLayout problem
Convert an array into Jlist
need help on JList