java handles the button click event

  • 2020-06-23 00:27:41
  • OfStack

Different event sources can produce different types of events. For example, a button can send an ActionEvent object, while a window can send an WindowEvent object.

Summary of the AWT time processing mechanism:

1. A listener object is an instance of a class that implements a particular listener interface (listener interface).

2. An event source is an object that can register a listener object and send the event object.

3. When an event occurs, the event source passes the event object to all registered listeners.

4. The listener object will use the information in the event object to determine how to respond to the event.

Here is an example of a listener:


ActionListener listener = ...;
JButton button = new JButton("OK");
button.addActionListener(listener);

The listener object is now notified whenever a button produces an action event. For buttons, as we think of it, an action event is the click of a button.

To implement the ActionListener interface, the listener class must have a method called actionPerformed that accepts one ActionEvent object argument.


class MyListener implements ActionListener
{
 ...;
 public void actionPerformed(ActionEvent event)
 {
   //reaction to button click goes here
 }
}

As soon as the user clicks the button, the JButton object creates an ActionEvent object and then calls listener. actionPerformed(event) to pass the event object. Multiple listener objects can be added to an event source such as a button. So, as soon as the user clicks the button, the button invokes the actionPerformed method of all listeners.

Example: Handles button click events

To deepen your understanding of the event delegate model, the following simple example of a response button click event illustrates the details you need to know. In this example, you want to place three buttons in a panel and add three listener objects to be used as action listeners for the button.

In this case, as soon as the user clicks any button on the panel, the associated listener object receives an ActionEvent object, indicating that a button has been clicked. In the sample program, the listener object changes the background color of the panel.

Before I show you how to listen for button clicks, I need to show you how to create buttons and how to add them to the panel.

You can create a button by specifying a label string, an icon, or both in the button constructor. Here are two examples:


JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton(new ImageIcon("blue-ball.gif"));

To add the button to the panel, call the add method:


JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");

buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);

Now that you know how to add buttons to a panel, you need to add code that lets the panel listen for those buttons. This requires a class that implements the ActionListener interface. As mentioned earlier, an actionPerformed method should be included, signed:

public void actionPerformed(ActionEvent event)

When the button is clicked, you want to set the background color of the panel to the specified color. This color is stored in the listener class:


class ColorAction implements ActionListener
{
  public ColorAction(Color c)
  {
   backgroundColor = c;
  }
  public void actionPerformed(actionEvent event)
  {
   //set panel background color
   }
   private Color backgroundColor;
}

Then, construct 1 object for each color and set these objects as button listeners.


ColorAction yelloAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);

yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);

For example, if a user clicks 1 on a button marked "Yellow", the actionPerformed method of the yellowAction object is called. The backgroundColor instance field for this object is set to ES72en.YELLOW, now set the background color of the panel to yellow.

There's one more thing to consider here. The ColorAction object cannot access the buttonpanel variable. There are two ways to solve this problem. One is to store the panel in the ColorAction object and set it in the constructor of ColorAction. The other is to make ColorAction the inner class of the ButtonPanel class, so that its methods automatically have access to the outer panel.

The following shows how to place the ColorAction class within the ButtonFrame class.


class ButtonFrame extends JFrame
{
 ...
 private class ColorAction implents ActionListener
 {
  ...
  public void actionPerformed(ActionEvent event)
  {
    buttonPanel.setBackground(backgroundColor);
  }
  private Color backgroundColor;
  }
  private Jpanel buttonPanel; 
}

Related articles: