Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

not able to get the items from JList

 
abhijeet shrivastava
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
abhijeet shrivastava
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only too pleased to help, and thank you, Ilja Preuss.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic