aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Referencing swing components from ActionEvent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Referencing swing components from ActionEvent" Watch "Referencing swing components from ActionEvent" New topic
Author

Referencing swing components from ActionEvent

Phillip Ankerson
Greenhorn

Joined: Aug 10, 2011
Posts: 27

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.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Aug 10, 2011
Posts: 27

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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Aug 10, 2011
Posts: 27

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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Aug 10, 2011
Posts: 27

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
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

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

Joined: Aug 10, 2011
Posts: 27

I figured it out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Referencing swing components from ActionEvent