title: How to fire an ActionEvent from a JPanel
body: <p>I'm trying to repaint a simple massage in a panel by firing an ActionEvent. </p>
  
  <p>I Have a <code>MessagePanel</code> that extends <code>JPanel</code>, in which I defined an <code>addActionListener</code> method and a <code>processEvent</code> method to process the event:</p>
  
  <pre><code>import java.awt.Graphics;
  import javax.swing.JPanel; 
  import java.util.*;
  import java.awt.event.*;
  
  public class MessagePanel extends JPanel {
      private String message = new Date().toString();
      ArrayList&lt;ActionListener&gt; actionListenerList;
  
      public MessagePanel(String message) {
          this.message = message;
      }
  
      public void setMessage(String message){
          this.message = message;
      }
  
      public void addActionListener(ActionListener listener) {
          if (actionListenerList == null) {
              actionListenerList = new ArrayList&lt;&gt;(2);
          }
          if (!actionListenerList.contains(listener)) {
              actionListenerList.add(listener);
          }
      }
  
      public void removeActionListener(ActionListener listener) {
          if (actionListenerList != null &amp;&amp;
                  actionListenerList.contains(listener)) {
              actionListenerList.remove(listener);
          }
      }
  
      public void processEvent(ActionEvent e) {
          ArrayList&lt;ActionListener&gt; list;
  
          synchronized(this) {
              if (actionListenerList == null) {
                  return;
              }
              list = (ArrayList&lt;ActionListener&gt;)actionListenerList.clone();
          }
  
          for (int i = 0; i &lt; list.size(); i++) {
              ActionListener listener = (ActionListener)list.get(i);
              listener.actionPerformed(e);
          }     
      }
  
      @Override
      protected void paintComponent(Graphics g){
           super.paintComponent(g);
           g.drawString(message, 0, 0);
      }
  }
  </code></pre>
  
  <p>Here's my test class:</p>
  
  <pre><code>import java.awt.event.*;
  import javax.swing.*; 
  import java.util.*;
  
  public TestMessaePanel extends JFrame {
      MessagePanel messagePanel = new MessagePanel(new Date().toString());
  
      public TestMessagePanel() {
          add(messagePanel);
          messagePanel.setCentered(true);
  
          messagePanel.addActionListener(new ActionListener(){
              @Override
              public void actionPerformed(ActionEvent e){
                  messagePanel.setMessage(new Date().toString());
              }
          });
      }
  
      public static void main(String[] args) {
          JFrame frame = new TestMessagePanelWithActionEvent();
          frame.setSize(300, 200);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>I'm trying to have the panel repaint the current time with every click of the panel (without using any other event sets) but I can't figure out what event is supposed to fire and invoke my <code>processEvent</code> method.  I'm not even really sure if I even need the <code>processEvent</code>, if I can just implement the process elsewhere. </p>
  
  <pre><code>EDIT WITH TEXTBOOK PROBLEM (below)
  </code></pre>
  
  <p>(Enable MessagePanel to fire ActionEvent) The MessagePanel class in
  Listing 15.7 is a subclass of JPanel; it can fire a MouseEvent,
  KeyEvent, and ComponentEvent, but not an ActionEvent. Modify the
  MessagePanel class so that it can fire an ActionEvent when an instance
  of the MessagePanel class is clicked. Name the new class
  MessagePanelWithActionEvent. Test it with a Java applet that displays
  the current time in a message panel whenever the message panel is
  clicked, as shown in Figure 36.9.</p>