1

Sorry but this is a concept that I never realized and I never used into my project. I need to learn and use it, absolutly.

So i read many articles about interface and event handling, but it doesnt keep in my mind.

Just start with an easy example :

public class Main implements ActionListener{
    JButton but=new JButton("BUTTON");
    public Main() {
        but.addActionListener(this);
    }


    public void actionPerformed(ActionEvent e) {
        // DO SOMETHINGS WHEN THE BUTTON IS CLICKED
    }
}

This code is absolutly easy. I implements the ActionListener interface, so i need to write my own code of his method (actionPerformed).

What i dont understand is :

1 - Who implements the addActionListener method? Its not a method on JButton class. Who provides this method?

2 - What is the bridge between the addActionListener and the actionPerformed method? The first should provide the Event e to the second... and both must be implemented somewhere...

Sorry for this question. I try to learn this (by reading many articles on internet) but i can't understand how this can work!

Cheers and thanks to everybody :)

4 Answers 4

5
  1. addActionListener is implemented by AbstractButton, the superclass of JButton.

  2. The JButton object holds a references to your ActionListener object (i.e. the instance of your Main class). When a UI event is triggered by the user, the JButton constructs an ActionEvent object and passes it to the stored ActionListener object, i.e. it passes the event to your Main class.

If you want to go into more detail, then look at the source code for AbstractButton.addActionListener to see what it does.

Sign up to request clarification or add additional context in comments.

9 Comments

Yes, in fact i just see that :) there is listenerList.add(ActionListener.class, l); Ok, so i pass to this method (add of listenerList) the class where there is the button (Main). Now i left to know who call actionPerformed :)
If you want to know who is calling "actionPerformed", you could just put a breakpoint in the "actionPerformed" method and check the call stack (or print out a stacktrace)
@markzzz: JButton (or its superclasses) call actionPerformed(), they're responsible for knowing who to notify when the user presses the button.
Uhm...but just for clearity. When i call but.addActionListener(this); , the this is meant as instance of the Main (Class) or the Interface? (sorry the tag shouldnt be so clear, but i don't know how to use the <code> tag here :))
@markzzz: Because Main is an ActionListener - it implements the interface. Your difficulties in understanding don't lie in the ActionListener and associated code, but with a more fundamental conception of what Java interfaces are.
|
2
1 - Who implements the addActionListener method? Its not a method on JButton class. Who provides this method?

JButton extends AbstractButton, inheriting the addActionListener from it.

2 - What is the bridge between the addActionListener and the actionPerformed method? The first should provide the Event e to the second... and both must be implemented somewhere...

In simplified terms: when particular area on screen is pressed, AWT event handler thread notifies the UI element located at that area of the screen.

In our case, if the UI element is the button, the button event handling logic loops over the list of action listeners (registered through "addActionListener") and calls "actionPerformed" method in each listener.

See How to Use Buttons, Check Boxes, and Radio Buttons in The Java Tutorial for information and examples of using buttons.

2 Comments

Ok the first point :) Point 2 : But how can happen this? I mean, how can it know that must call "actionPerformed" after addActionListener() call? Someone must tell this information...
It does not call the "actionPerformed" after the "addActionListener" - these are two independent events. "addActionListener" is used just to register all parties that are interested in getting notifications from the particular button. "actionPerformed" is called by a totally different thread (AWT event handler thread) when the particular button is actually pressed.
1

Who implements the addActionListener method? Its not a method on JButton class. Who provides this method?

addActionListener is a method on the AbstractButton parent class.

What is the bridge between the addActionListener and the actionPerformed method? The first should provide the Event e to the second... and both must be implemented somewhere...

The class that implements the ActionListener method implements actionPerformed. It's the one that wishes to be notified about and respond to events.

2 Comments

Thanks to your reply :) Fine, actionPerformed is the one that wishes to be notified, but why that method? Who said "use actionPerformed" and not somethings others? :)
The interface demands it. If you don't implement that method the compiler will complain.
1

Your first question is answered, so answering the second: the bridge is JButton.setActionCommand(String command); and ActionEvent.getActionCommand();. This is useful when an actionlistener listens for multiple buttons.

3 Comments

Uhm Thanks. Is still don't understand the relationship from the Main class and the Interface... :)
@markzzz Which part? You can implement ActionListener anywhere else, or as an anonymous class.
this : When i'll implements my own event handler/interface, i'll pass to the addSomeListener the instance of my "formal" class (that implements method of SomeListener (for example actionReturned). How can I call that actionReturned to the class that demand that resource?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.