1

Well,as far as i know, the event object passed as an argument to the event handler function in Java, holds the additional information about the event. So, that means it might not be important at times to pass that argument until we require it.

But then, that's not the case...since it throws an error if the object parameter is missing. For example, this would throw an error..

public void actionPerformed() {     //since the event object parameter is missing

        button.setText("Clicked");
    }

why is it so? It's just a waste at times to pass the parameter, so why is there no method in the Swing/AWT Java API that allows event handlers consisting of no parameters at all? That would really comfort a lot. NO?

Juzz Coding
  • 321
  • 1
  • 11
  • 1
    It doesn't "thrown an error". Rather it's a compilation error because a particular interface hasn't been implemented - make sure to *read* and *post* the actual error indicators (this is why I downvoted). If you make your *own* events/event handlers, then you can specify whatever parameters are required (possibly none); but if you use *existing* events, then you have to play by their defined rules. – user2864740 Nov 13 '13 at 05:13
  • @user2864740 can we create our own event handler? – Juzz Coding Nov 13 '13 at 05:20
  • Sure, if you create your own even source (maybe a control?). See http://stackoverflow.com/questions/6270132/create-a-custom-event-in-java – user2864740 Nov 13 '13 at 05:27
  • @user2864740 That's really useful! :) – Juzz Coding Nov 13 '13 at 05:33

1 Answers1

5

Yes, it's important, the parameter forms part of the contract as stated by the interface.

An interface describes what is expected of implementations.

public interface ActionListener {
    public void actionPerformed(ActionEvent evt);
}

In order for a class to be able to call any implementation of ActionListener, it must provide not only the name of the method, but any parameters that it (the interface) lists.

public void actionPerformed() { 
    button.setText("Clicked");
}

Does not meet the requirements of the contract for the ActionListener and therefore can not be called. In fact, assuming that you class implements ActionListener, this will not compile unless you provide an implementation of the form method signature (public void actionPerformed(ActionEvent evt)).

If you don't want to use the evt object, then ignore it.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366