Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4477608

java.awt.Component.processKeyEvent is incomplete.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • docs
    • beta2
    • sparc
    • solaris_2.6



      Name: dsR10051 Date: 07/09/2001


      The method
      protected void java.awt.Component.processKeyEvent(KeyEvent e)
      is not called by event dispatch thread if component is not focus owner.
      This behavior has been introduced in jdk 1.4 by new focus management,
      but hasn't been described in documentation for this method yet.
      Javadoc says:
          /**
           * Processes key events occurring on this component by
           * dispatching them to any registered
           * <code>KeyListener</code> objects.
           * <p>
           * This method is not called unless key events are
           * enabled for this component. Key events are enabled
           * when one of the following occurs:
           * <p><ul>
           * <li>A <code>KeyListener</code> object is registered
           * via <code>addKeyListener</code>.
           * <li>Key events are enabled via <code>enableEvents</code>.
           * </ul>
           * <p>Note that if the event parameter is <code>null</code>
           * the behavior is unspecified and may result in an
           * exception.
           *
           * @param e the key event
           * @see java.awt.event.KeyEvent
           * @see java.awt.event.KeyListener
           * @see #addKeyListener
           * @see #enableEvents
           * @since JDK1.1
           */
          protected void processKeyEvent(KeyEvent e) {
      So, method is called when key eventes are registered via addKeyListener
      or enableEvents methods but the implementaion of EventDispatchThread
      redirects KeyEvent to focus owner, it should be documented. See also bugs:
      #4407789, #4379917, #4391517, #4392099 for more details.
      __________________________

      Here is minimized example:

      import java.awt.*;
      import java.awt.event.*;

      public class ComponentTest8 {
          public static void main (String[] args) {
              EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
              ChildComponent chcomponent = new ChildComponent();
              KeyEvent e = new KeyEvent(chcomponent, KeyEvent.KEY_RELEASED, (long)0, 0, KeyEvent.VK_UNDEFINED);
              DummyKeyListener cl = new DummyKeyListener();
              chcomponent.addKeyListener(cl);

              eq.postEvent(e);
              synchronized (chcomponent) {
                  while(!chcomponent.keyEventProcessed) {
                      try {
                          System.out.println("Waiting");
                          System.out.flush();
                          
                          chcomponent.wait();
                          
                          System.out.println("Waiting");
                          System.out.flush();
                      } catch (InterruptedException ie) {
                          ie.printStackTrace();
                      }
                  }
              }
              System.exit(0);
          }
      }

      class ChildComponent extends Component {
          public ChildComponent() {
              super();
          }

          boolean keyEventProcessed = false;
          public synchronized void processKeyEvent(KeyEvent e) {
              keyEventProcessed = true;
              System.out.println("processKeyEvent called");
              System.out.flush();
              super.processKeyEvent(e);
              System.out.println("processKeyEvent done");
              System.out.flush();
              notifyAll();
          }
      }

      class DummyKeyListener extends KeyAdapter {
      }

      --- Output ---

      %/set/java/jdk1.3.1/solaris/bin/java ComponentTest8
      Waiting
      processKeyEvent called
      processKeyEvent done
      Waiting
      %/set/java/jdk1.4/solaris/bin/java ComponentTest8
      Waiting
      ^C

      ======================================================================

            sharonz Sharon Zakhour (Inactive)
            sdasunw Sda Sda (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: