title: How do I redispatch ANY AWTEvent in java?
body: <p>I have a feature to implement in a Java 1.5 Swing based application. 
  If a particular exception happens when processing an AWTEvent, I have to popup an alternate form, resolve the problem and continue processing the original event.
  When I redispatch the event to the component nothing happens. When I push the event into the event queue nothing happens. I assume there is some state fields in the event which mark it as processed, so components don't pick it up.
  So far I cannot find a way to recreate a clone of the event. And a custom event will not help here because I want the previous event to get processed.</p>
  
  <p>In the swing application, the existing event queue gets replaced by an internal queue.</p>
  
  <pre><code>private class ApplicationEventQueue extends EventQueue  
  {  
      private final ArrayList listeners=new ArrayList();  
      protected void initialize()  
      {  
          Toolkit.getDefaultToolkit().getSystemEventQueue().push(this);  
      }
  .
  .
  .
  }</code>
  </pre>
  
  <p>As part of the dispatch event, The class intercepts the call ans delegates to the super class. If an exception occurs it will pop-up a messagebox with a "sorry for the inconvienience" message.  </p>
  
  <pre><code>@Override  
  protected void dispatchEvent(AWTEvent event)  
  {  
      try  
      {  
          super.dispatchEvent(event);  
          if (peekEvent() != null && userEventDispatched)  
          {  
              raiseIdleEvent();  
              userEventDispatched = false;  
          }  
          else  
          {  
              int eventId = event.getID();  
              if (eventId == KeyEvent.KEY_TYPED || eventId == MouseEvent.MOUSE_CLICKED)  
              {  
                  userEventDispatched = true;  
              }  
          }  
      }  
      catch (Throwable ex)  
      {  
          onError(ex);  
      }  
  }</code></pre>
  
  <p>The feature required is to be able to timeout a user session. The server will throw a specific exception when the session times out. On timeout the user is prompted to relogin and the original action that was aborted will proceed. 
  What I wanted to do is, As part of the onError I will handle the exception by displaying a form. That particular event would be consumed but after re-authentication, I can redispatch the same event to the Application or perhaps push it into the event queue. 
  Both approaches have failed since I presume the event has flags that indicated whether it was posted and consumed.</p>
  
  <ol>
  <li>The event coming through could be any event (whether mouse of a keystroke).  </li>
  <li>Defining a custom event would not solve them problem since I need to replay the same event. </li>
  <li>I have considered cloning the event but cloning is not supported by AWTEvent.  </li>
  <li>Deep copy by serializing and then de-serializing the event did not work as some events being dispatched are not serializable.  </li>
  <li>I am considering resetting any state variables in the event by reflection but it seems dangerous.  </li>
  </ol>
  
  <p>Sorry for the lousy formatting, I haven't figured out the markup yet.
  Any help here would be appreciated.</p>
  
  <p>FIXED: Thank you for all the answers. The fix (not found by myself) was to catch the session timeout exception when the call was made. The application popped up a dialog and asked the user to re-authenticate. After the authentication was successful, the dialog was closed. It worked to my surprise.</p>
  
  <p>I do not know for certain but it seems like the event remained stuck in the queue when the dialog was shown and once the dialog was closed, it got delivered to the controls it was going to be anyway.</p>