• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Referencing swing components from ActionEvent

 
Greenhorn
Posts: 27
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to paste my code below and realize that it is subject to many, many improvements, corrections, suggestions, etc, all of which I will appreciate. I'm trying my best to learn swing.

I have a frame with two panels; first panel has a textbox which user enters a value into. Second panel has the 'GO' button to perform the action. When the user presses the button, the code needs to perform some action using the value in the textbox from the first Panel. I am getting a 'cannot find symbol' error referencing line 159 of my code. I don't know how to reference the swing components from the ActionEvent code, obviously they are not in scope.

 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Phillip Ankerson wrote:obviously they are not in scope



Precisely. The one you mentioned is a local variable in the class's constructor. If you want it to be accessible throughout the class, then don't do that. Declare it at class level, as an instance variable of the class.
 
Phillip Ankerson
Greenhorn
Posts: 27
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course I'm confused...it seems that jUPCBlockText is out of scope and not upcBlock, but jUPCBlockText is a component and not a declared variable.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Phillip Ankerson wrote:jUPCBlockText is a component and not a declared variable.



It's declared at line 44 of your post. As a local variable in the constructor of the class.
 
Phillip Ankerson
Greenhorn
Posts: 27
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, as a swing component. Should it not be done this way? The component is part of the frame so I thought it had to be here...

Can you smell the smoke?
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you want it to be accessible throughout the class, then ... Declare it at class level, as an instance variable of the class.



You seem to be mixing up the Java code with the design of the GUI. Yes, there are components in your GUI. And they are represented as Java objects in your code. I think part of the problem is that you're letting somebody else write your code and that, as a result, you don't understand what the code is all about. If you stopped doing that for a bit and went through the Swing tutorials, you might get a bit better feeling for how it's actually done.
 
Phillip Ankerson
Greenhorn
Posts: 27
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have gone through the Swing tutorials. They are vanilla and you don't really get a good feel for putting something together that you can really use. I'll keep plugging away at it.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the meantime why don't you just take that code, find all the components which you want to manipulate from various methods in the class, and change the code so those components are declared as instance variables?
 
Phillip Ankerson
Greenhorn
Posts: 27
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!