Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: Question in ActionEvent

  1. #1
    WaseemSwaileh is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Question in ActionEvent

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class SimpleGui1 implements ActionListener {
    
        JFrame frame;
        JButton button;
    
        public static void main(String[] args) {
            SimpleGui1 gui = new SimpleGui1();
            gui.go();
    
        }
    
        public void go() {
            frame = new JFrame();
            this.button = new JButton("Click Me");
            this.button.addActionListener(this);
            this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.frame.getContentPane().add(this.button);
            this.frame.setSize(300, 300);
            this.frame.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
                this.button.setText("I've Been Clicked");
        }
    }
    Good Day Dears .. I start learning java around 3 mouths ago .. and I have come across this multiple times and I know how to write the code for Listening to a button .. But I think I am missing something here ..

    Java Code:
        @Override
        public void actionPerformed(ActionEvent e) {
                this.button.setText("I've Been Clicked");
        }
    I know that .. the above code is coming from the interface ActionListener that we have to override it's solo method.
    but what exactly the ActionEvent Object and how it's being passed through the object that we created .. ?

    I mean .. my problem of understating starts once the button is being clicked .. the questions followed the click are .. how Java take the ACTION of clicking the button and send it to the actionPerformed method .. and what is object is being passed to ActionEvent and why we store that object to (e) variable .. and we don't even use it? and what is the ActionEvent Object anyway ? .. Please can someone explained to me in a boring details .. and I will be thankful .

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Question in ActionEvent

    When the button is pressed (the event) the JVM creates the ActionEvent object and calls the actionPerformed() method passing it a reference to that object as an arg to the method.
    Read the API doc for the ActionEvent class and the classes that it extends to see what is in the object that is passed.
    WaseemSwaileh likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    WaseemSwaileh is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Question in ActionEvent

    Thanks a lot dear .. that was really helpful

Similar Threads

  1. GUI waiting for ActionEvent
    By Kimya in forum New To Java
    Replies: 5
    Last Post: 07-01-2012, 06:07 AM
  2. Wait for ActionEvent
    By Razion in forum New To Java
    Replies: 2
    Last Post: 05-31-2012, 01:08 PM
  3. What does (this) and ActionEvent e mean?
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 06:08 PM
  4. StringBuilder/ActionEvent help!
    By Red727 in forum New To Java
    Replies: 8
    Last Post: 12-07-2010, 04:01 AM
  5. ActionEvent example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:00 AM

Posting Permissions

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