What does ActionEvent e mean?
Asked Answered
K

3

5

I am learning Java and would really like to have a deeper understanding of what the ActionEvent e perameter means and stands for. When I code I don't just want to spit out lines that work, but I don't understand. I want to have a full understanding of concepts before I use them.

So what specifically is it asking for and what do the two parts(ActionEvent and e) mean?

class ButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
    }
}

Thanks.

Klehm answered 4/7, 2015 at 1:41 Comment(2)
e is a type of ActionEvent, it allows you to access the properties of the ActionEvent. The actionPerformed method is called when the associated object generates a action, the ActionEvent carries the properties if the action which help you identify what you should doBrandenburg
In short..ActionEvent is class or event, that gets complete references when event will be fire. So that you can get complete information about fired event and use it's function,properties and all others.Acetometer
S
7

ActionEvent is a class, e is an instance of that class. You can use e to call it's methods/properties, found here

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionEvent.html

ActionEvent is just a type, it informs you what type of object e is. By the way, you can change e to whatever you prefer, for ex. event or object.

ActionEvent event, ActionEvent object (remember, not to be confused with Object, its object with lower case "o"), ActionEvent anyVariableName etc...

Then inside actionPerformed() you can call stuff like event.doSomething();

Sisyphus answered 4/7, 2015 at 1:52 Comment(3)
so I could change the "e" to any name I want?Klehm
Yes, it's just a reference name, a variable. It's instantiating the class ActionEvent, so you cannot change ActionEvent. "e" only has meaning within actionPerformed().Sisyphus
This is a fairly advanced topic to start learning Java on. I would not jump right into learning event driven programming without first learning about the basics of object oriented programming, I would focus on more simple examples within the main method, to learn what object oriented program is, why its used. After that try studying the observer design pattern, then you would be better equipped to understand event-driven programming.Sidman
V
0

The ActionEvent is an "event" that your listener catches, as sent by a dispatcher. This mean, in layman's terms, that some thread somewhere has decided that your actions (i.e. clicking a button, etc.) have caused an action to occur, and informs the System. Your listener picks up on this, and takes a reference as the parameter e. This may help to shed a bit more light on what/why the action is; and, it may be beneficial to check out the Event Dispatch Thread (EDT).

Valueless answered 4/7, 2015 at 1:50 Comment(0)
B
0

This should help you: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Basically,ButtonListener is your ActionListener Implementation. You will use it like

someButton1.addActionListener(new ButtonListener());
someButton2.addActionListener(new ButtonListener());

It will listen for any action events on the buttons 'someButton1' and 'someButton2'.But we might want to handle clicks on both buttons in a different way. Thats when ActionEvent is of use.

Inside method,we can do this by following

@Override
public void actionPerformed(ActionEvent e){
    if(e.getActionCommand().equals("Button 1")){
        //Process Button 1 action event here 
    }
    else if(e.getActionCommand().equals("Button 2")){
        //Process Button 2 action event here 
    }

}
Blumenfeld answered 4/7, 2015 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.