The Artima Developer Community
Sponsored Link

Java Answers Forum
Get string from icon and string list

4 replies on 1 page. Most recent reply: Mar 1, 2004 5:58 AM by Daniel Liu

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Daniel Liu

Posts: 11
Nickname: hhliu
Registered: Feb, 2004

Get string from icon and string list Posted: Feb 28, 2004 8:00 AM
Reply to this message Reply
Advertisement
Hello,

I need print the string when I select an icon and string JList.

The following is the code segment I wrote:

Object personValue = personList.getSelectedValue();

System.out.println(personValue.getText().toString());

// personList has labels with icons and strings.

When I compile the code, it shows:

cannot resolve symbol
symbol : method getText ()
location: class java.lang.Object
System.out.println(personValue.getText().toString());
^
1 error

Also, I tried System.out.println(personValue.getString());
It shows the same error.

Could any one tell me how to solve this problem?

Thank you,

Daniel


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Get string from icon and string list Posted: Feb 28, 2004 2:06 PM
Reply to this message Reply
The message "cannot resolve symbol" means that the compiler cannot find a method by the name of getText() in the Object class. If you look at the documentation of the Object class, you will see that it doesn't contain any such method. Which is why you got the message.

The method returns an Object because Object is the most generic of all object types. However, what is being returned is more than a plain-vanilla Object. You need to cast it to whatever it actually is. Without seeing your actual code, I can't be sure what to cast to. But according to the documentation of JList, it appears that the individual cells are rendered as JLabel's.

Try something like this first.
JLabel personValue = (JLabel) personList.getSelectedValue();
 
System.out.println(personValue.getText());

If that doesn't work, you may need to post more code.

Daniel Liu

Posts: 11
Nickname: hhliu
Registered: Feb, 2004

Re: Get string from icon and string list Posted: Feb 29, 2004 2:46 AM
Reply to this message Reply
Thanks. I tried the following code:

JLabel personValue = (JLabel) personList.getSelectedValue();

System.out.println(personValue.getText());

It shows the following message when it's running:
java.lang.Integer

I think the returned object should be a JLabel,
but why it returned back an Integer?

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Get string from icon and string list Posted: Feb 29, 2004 8:14 AM
Reply to this message Reply
Without seeing more of your code, I'm not able to tell. If you can post more, I'll see if I can help.

twc

Daniel Liu

Posts: 11
Nickname: hhliu
Registered: Feb, 2004

Re: Get string from icon and string list Posted: Mar 1, 2004 5:58 AM
Reply to this message Reply
Thanks.

I have solved the problem.

I found the original object I put into the JList is Integer.

Now, I put JLabel into the JList, so it works now.

Again, thank you very much for your help.

Flat View: This topic has 4 replies on 1 page
Topic: Action Performed lotto generator Previous Topic   Next Topic Topic: Thanks (Help with counter)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use