Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A little problem..

 
Ben Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there! I'm new to this any trying to get a GUI sorted out. I've been trying to do Event Handlers but for some reason, I keep getting an error saying the variable can't be found. My code is below:

 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You would have been better to post on the Swing Forum, but the problem is that the JButton you are calling the action on is declared as a local variable, so it is out of scope when you get to the actionPerformed method.
 
Ben Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Richie, thank you. I've realised (Apparently my spelling of 'realized' isn't right =]) the error of my ways. I declared the button globally. Sorry about posting in wrong section, first post so forgive me please =].
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Brownlow:
'realized' isn't right
If only I could get my browser to mark "realised" as correct and "realized" as wrong.
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and I trust you have got it working now?
 
Ben Jones
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops.. Sorry about spelling your name wrong also. Yea', all is working now, declared all the 'JButtons' globally so that all my methods can use them. I really should of noticed that =]

Thanks for the help.
 
Campbell Ritchie
Sheriff
Posts: 48440
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everybody declares things as local variables from time to time. Don't call them "global" however. That is a C/C++ concept; a C "global" is "visible" to any code in the application. In OO programming you want to declare your fields as "private" so they are only visible within your class.

And no problem about the other things you mentioned!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic