This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JLabels and setText() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabels and setText()" Watch "JLabels and setText()" New topic
Author

JLabels and setText()

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I added a JLabel to a frame, and attempted to change it's text using .setText() inside a mouseEvent (click). When I click inside a circle I created, the JLabel doesn't change, instead the console is bombarded with error messages. How can I get my JLabel to change when a shape is clicked on?



Then inside my Mouse listener I have

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

My guess: you declared player_position_label as an instance variable in your class (code you didn't post), but didn't assign it a value. Then you declared a local variable named player_position_label (in the code you posted) inside a method. Then using player_position_label from a different method would use the instance variable, and that would produce a NullPointerException (in the stack trace you didn't post).

But that's just a guess. Knowing what the exception was would help.
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
Yes, I do get a nullpointer exception at the line where I try performing the setText() on the JLabel inside the mouseListener. Along with tons of other error messages which I guess are related to that one error. I created the JLabel and set the text to ("Click a player"). Added it to the frame, declared it as a private instance variable inside the class. Then inside the mouseListener I try calling the instance variable. I thought once I created the label, as long as I referenced it's name I could setText() anywhere. I want the label to say "Click a player", and then change when a player is clicked. How do I have to code it, so it will work?

Russell Peters
Ranch Hand

Joined: Jan 25, 2006
Posts: 50
Originally posted by John Lockheart:
Yes, I do get a nullpointer exception at the line where I try performing the setText() on the JLabel inside the mouseListener. Along with tons of other error messages which I guess are related to that one error. I created the JLabel and set the text to ("Click a player"). Added it to the frame, declared it as a private instance variable inside the class. Then inside the mouseListener I try calling the instance variable. I thought once I created the label, as long as I referenced it's name I could setText() anywhere. I want the label to say "Click a player", and then change when a player is clicked. How do I have to code it, so it will work?



Paul was right on the money there !

The JLabel that you have in the constructor, is a local variable to that constructor, while the JLabel that you are trying to use in the mouse listener is the member variable.

You need to change the constructor from this


to this


provided you haven't defined "player_position_label" elsewhere in your listener


SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JLabels and setText()
 
Similar Threads
Updating a JLabel on a JPanel
X and O game
Explicit Event Handling
Explicit Event Handling
Buttons...