• Post Reply Bookmark Topic Watch Topic
  • New Topic

unable to access variables

 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was creating a swing applet if i make a seperate class of action listener i was unable to access extended class variables.Can someone explain my mistake and modify the code without merging two classes.





 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ActionEvent has a method getSource() which gives you the component from which this event originated. In your case it would be the button
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what you exactly want to say
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i know which button triggered the event as the actionlistener belongs to that button particularly , why i cannot access that button directly
 
jeevansai jinne
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to access some variable other than button in actionlistener class of button
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that code compile?
 
Fred Kleinschmidt
Bartender
Posts: 457
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(NOTE: Class names should begin with a capital letter). As long as your listener class is an inner class of your Clock class, you access other variables the same as you accessed the button variable. You have `button.setText()` already; you can also refer directly to 'frame and panel, or any other non-private instance variables that you declare.
 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred,

In programming most problems, other than syntax and logic, are rooted in visibility, life, and scope. You have a class that creates the Buttons and adds the ActionListeners, The buttons are created in the same scope as the instances of the actionlisteners, so they are easily put together, but now you want to access that button and its classes or perhaps other objects in your actionlistener. You cannot because from within your actionlistener you do not have visibility to the other objects. The simple question arises: how do I get visibility to what I want to see from the scope of my actionlistener? Plain and simple: in Java you have to get a reference to that object. Someone already mentioned that you can get a reference to the object from the event--getSource will give you a reference back to the object that is the caller, originator, of the event.

So what about other things that you want to get visibility to? Well, at the time of instantiation of your actionlistener object any other object that is live and is visible in the scope where your actionlistener is instantiated can give you a reference--so basically if the scope the actionlistener is created in has visibility to a live object, then it can give a reference.

Now the catch comes in--how do you get a reference to any object that is live and visible in the scope at time of instantiation? You have to have a reference passed in, either in a constructor or a setter, something like this:


That is basically how it all works with gaining visibility to objects in other objects, other than getSource from your event. Yes, there are other ways too, but not nearly so nice and direct as this.
 
Fred Kleinschmidt
Bartender
Posts: 457
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create the action listener as an anonymous inner class of of the class that contains the button. Then you have access to all of the methods of that class, and all of its instance variables. Also you can have the action listener just call a method fo that class to perform the action:

 
Henry Wong
author
Sheriff
Posts: 22532
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we have gone a little long to get to the explanation, with ...

Fred Kleinschmidt wrote:Create the action listener as an anonymous inner class of of the class that contains the button. Then you have access to all of the methods of that class, and all of its instance variables.


and from the original post ...

jeevansai jinne wrote:I was creating a swing applet if i make a seperate class of action listener i was unable to access extended class variables.Can someone explain my mistake and modify the code without merging two classes.




Let's put them together. Anonymous inner classes (and method local inner classes) that are instantiated in the scope of an instance method will get the "this" instance as its outer class -- which as Fred mentioned, will give "access to all of the methods of that [outer] class, and all of its instance variables" to the inner class.

Henry
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original code looks fine except one thing. Why is the action listener class needs to extend Clock?

Class names should be capitalized.
 
Piet Souris
Rancher
Posts: 1635
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes it is not so easy to make a panel full of components (buttons e.g.) an innerclass of
some other class.

Think of the situation where you have a main program with a frame, to which you want to add
a dedicated panel full of buttons and other stuff. Then it is possible to do the layout of this
panel with a GUI builder. The code generated by such a GUI builder is too horrible to copy it
into the main code.

In that case, since that panel is highly tied to your frame, you can let the relevant components
have protected access. You can then simply call them from your main code, add actionListeners,
whatever you like.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!