Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing label text when button is clicked  RSS feed

 
Ethan Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
I am trying to make the label change text when the button is clicked I think the problem is the JButton button = new JButton("Change Label"); , I don't think the method to change the text of the label can see the button , any help is appreciated thanks guys here is the code I have so far


 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't make frames implement action listener. Use a class (or a λ) for the listener. In the action performed method, you have to set the text on the target component. You will find that most Components have a setText method.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course what you have done won't compile because the label is a local variable. You can change it to a field, or you can create a TextChangeListener and pass the label as an argument to its constructor. It is the label that is the problem, not the button.
 
Ethan Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Of course what you have done won't compile because the label is a local variable. You can change it to a field, or you can create a TextChangeListener and pass the label as an argument to its constructor. It is the label that is the problem, not the button.


Great got it working, thanks a lot for your help I appreciate it
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done How did you sort it out?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!