What is the name of the method that receives ActionEvents when a Button is clicked?

A good answer might be:

actionPerformed( ActionEvent e )

More on actionPerformed( )

Here is this method from the example program:

public class buttonDemo extends Frame implements ActionListener
{
  . . . .
     
  public void actionPerformed( ActionEvent evt)
  {
    setBackground( Color.blue );
    repaint();                           // ask the system to paint the screen.
  }

  . . . . 
}

The parameter is a reference to an ActionEvent object. When the button is clicked an ActionEvent object, representing the action, is sent to the method. Our method does not use the information in the ActionEvent object it receives, but in other programs this information is used. You will see this shortly.

Usually actionPerformed() does something more significant than it does in this program. Most useful programs have some application code (as well as GUI components and event listeners.) Often various sections of the application code are activated with button clicks. In a real application, the method might look something like this:

public class buttonDemo extends Frame implements ActionListener
{
  . . . .
     
  public void actionPerformed( ActionEvent evt)
  {

    //look at information in the ActionEvent

    //invoke a method that is part of the application code 

    //send the results of that method to another GUI component
    //(perhaps a text field) 

    repaint();                           // ask the system to paint the screen.
  }

  . . . . 
}

Things can get quite complicated.

QUESTION 12:

Is it clear how:

  • the container buttonDemo,
  • the Button it contains,
  • the ActionListener registered for the Button,
  • and the WindowListener, registered for the container
are all related?

Click Here after you have answered the question