Results 1 to 4 of 4
- 02-06-2011, 11:42 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
What does (this) and ActionEvent e mean?
I have come across this a number of times and have simply memorised how it is written but I have very little (if any) understanding of what is actually going on, something I find hugely frustrating. What do we mean when we pass (this) to the parameters? Again when we pass (ActionEvent e) what do we mean? Again I have seen it written this way (ActionEvent evt)Java Code:public class Beeper extends JFrame implements ActionListener { button.addActionListener(this); public void actionPerformed(ActionEvent e) { //something happens } }
I have tried reading on this matter and have heard talk of overriding methods and other seemingly mind boggling terms but I have not fully grasped this. I am not content simply writing things I do not understand.
I would appreciate a link to a helpful resource that could help me in this regard or, better yet, if someone who understands this spares a moment to explain this as though they were speaking to a rather dull 15year old :D
Many thanks in advance for your help.
- 02-06-2011, 12:00 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
'this' in the context of a non-static method refers to the object itself; it is analogous to the following:
In your code the object sets itself as the ActionListener to a Button. It implements the ActionListener interface so it has to implement the actionPerformed(ActionEvent ae) method.Java Code:private int property; public void setProperty(int property) { this.property= property; // <-- this is the object itself }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-06-2011, 02:40 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
In simple:
When you want your component to perform some action (in your case click on a button) you asign an ActionListener to that component. Now when action occurs, this ActionListener must know who is going to response to that action so you have to state that like you did in this line:
Java Code:button.addActionListener([I][B]this[/B][/I]);
(this) means you want your class Beeper to handle the action. So you must insert
Java Code:public void actionPerformed(ActionEvent e) {
method inside your Beeper class and this method is actualy going to do what you want.
To be able to perform actions your class Beeper must implement ActionListener interface and you do this with:
Java Code:class Beeper [I][B]implements ActionListener [/B][/I]
Or you can declare someone else to do a job so your code could looks like:
Java Code:public class Beeper extends JFrame { button.addActionListener([B][I]new MyActionHandler()[/I][/B]); // here you define an inner class for handling the action class [I][B]MyActionHandler[/B][/I] implements [B]ActionListener[/B] { public void actionPerformed(ActionEvent e) { //something happens } } }
In this case you declared MyActionHandler to response to action and so MyActionHandler have to implement both ActionListener and actionPerformed() method.
The flow is next: when action occurs on button his actionListener activates MyActionHandler class which executes actionPerformed() method.
actionPerformed() gets a parameter of type ActionEven which you can name as ever you wish, so basicly next line is legal:
Java Code:public void actionPerformed(ActionEvent milovan) {
In case you need to do something with ActionEvent you can reffer to it inside you actionPerformed() method. For example:
Java Code:public void actionPerformed(ActionEvent milovan) { System.out.println("Action command name is: " + milovan.getActionCommand(); //something happens }
This is of course over simplified explenation on the topic and is intended just to give you some understanding on the matter. There are a lot of usefull tutorials on this subject so do a little research...
- 02-06-2011, 05:08 PM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
StringBuilder/ActionEvent help!
By Red727 in forum New To JavaReplies: 8Last Post: 12-07-2010, 03:01 AM -
newbi. ActionEvent. Close on click
By ocean in forum New To JavaReplies: 3Last Post: 09-18-2009, 09:41 AM -
Problems with ActionEvent for JButton
By TrueBear in forum AWT / SwingReplies: 1Last Post: 08-24-2009, 04:26 PM -
[SOLVED] Actionevent problem
By Cymro in forum New To JavaReplies: 3Last Post: 04-04-2008, 07:11 AM -
ActionEvent example
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks