• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JLabels and setText()

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Sheriff
Posts: 26946
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic