Results 1 to 4 of 4
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default What does (this) and ActionEvent e mean?

    Java Code:
    public class Beeper extends JFrame implements ActionListener {
       
            button.addActionListener(this);
      
            public void actionPerformed(ActionEvent e) {
    
             //something happens
    
            }
    
    }
    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)

    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.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by africanhacker View Post
    Java Code:
    public class Beeper extends JFrame implements ActionListener {
       
            button.addActionListener(this);
      
            public void actionPerformed(ActionEvent e) {
    
             //something happens
    
            }
    
    }
    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)

    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.
    'this' in the context of a non-static method refers to the object itself; it is analogous to the following:

    Java Code:
    private int property;
    
    public void setProperty(int property) {
       this.property= property; // <-- this is the object itself
    }
    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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    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...

  4. #4
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

Similar Threads

  1. StringBuilder/ActionEvent help!
    By Red727 in forum New To Java
    Replies: 8
    Last Post: 12-07-2010, 03:01 AM
  2. newbi. ActionEvent. Close on click
    By ocean in forum New To Java
    Replies: 3
    Last Post: 09-18-2009, 09:41 AM
  3. Problems with ActionEvent for JButton
    By TrueBear in forum AWT / Swing
    Replies: 1
    Last Post: 08-24-2009, 04:26 PM
  4. [SOLVED] Actionevent problem
    By Cymro in forum New To Java
    Replies: 3
    Last Post: 04-04-2008, 07:11 AM
  5. ActionEvent example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •