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

Swing KeyListener events are not consumed with user defined KeyEventDispatcher

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 1.4.0
    • 1.4.0
    • client-libs
    • None
    • beta
    • generic
    • generic, windows_98
    • Verified

      With the new focus model, users may define custom KeyEventDispatcher to process events. KeyEventDispatcher may be also be set to consume the event by returning TRUE from the dispatchKeyEvent() method.

      If TRUE is returned, the KeyboardFocusManager should take no further action with regard to the KeyEvent.

      When implementing a custom KeyEventDispatcher with Swing components, and returning TRUE from dispatchKeyEvent() method, the keyTyped event is still being received by the Swing component JTextField.

      - This problem is not occuring with AWT components (only Swing)
      - This problem is only occuring Win32 (not Solaris or Linux)
      - Bug occured with build 1.4beta-B42
      - Focus Test Suite:
      MerlinFocus/Automated/Swing/KeyEventDispatcherTest/

      In the sample code provided, a customized DefaultKeyboardFocusManager and KeyEventDispatcher were used to monitor GUI events from JTextField. Note that for Win32, a keyTyped event was received by the JTextField. Interesting to note that no keyPressed and keyReleased events were not seen though.

      - Steps to reproduce:

      1) Compile and run the KeyEventDispatcherBUG2.java sample below

      2) Type any text key within TextField you will notice that keyTyped event was triggered for JTextField. This event should have been consumed by custom KeyEventDispatcher.

      3) The sample program will print out all events received.

      KeyEventDispatcherBUG2.java
      --------------------------
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      class KeyEventDispatcherBUG2 implements KeyListener {

      JFrame frame;
      JTextField textfield1, textfield2;

      CustomDefaultKeyboardFocusManager customKeyboardFocusManager =
      new CustomDefaultKeyboardFocusManager();

      CustomKeyEventDispatcher2 customKeyEventDispatcher2 =
      new CustomKeyEventDispatcher2();

      public KeyEventDispatcherBUG2() {

      frame = new JFrame("Test Frame");

      textfield1 = new JTextField("TextField1");
      textfield2 = new JTextField("TextField2");

      frame.setSize(300,100);

      // Add key listener for the text field
      textfield1.addKeyListener(this);
      textfield2.addKeyListener(this);

      frame.getContentPane().setLayout(new FlowLayout());
      frame.getContentPane().add(textfield1);
      frame.getContentPane().add(textfield2);

      // Set our keyboard focus manager
      KeyboardFocusManager.setCurrentKeyboardFocusManager(customKeyboardFocusManager);
      customKeyboardFocusManager.addKeyEventDispatcher(customKeyEventDispatcher2);

      frame.setVisible(true);

      textfield1.requestFocus();
      }

      public void keyPressed(KeyEvent ke) {
      System.out.println("Event: keyPressed");
      }

      public void keyReleased(KeyEvent ke) {
      System.out.println("Event: keyReleased");
      }

      public void keyTyped(KeyEvent ke) {
      System.out.println("Event: keyTyped");
      }

      public static void main(String[] args) {
      new KeyEventDispatcherBUG2();
      }
      }


      class CustomDefaultKeyboardFocusManager
      extends DefaultKeyboardFocusManager {

      public boolean dispatchKeyEvent(KeyEvent ke) {
      System.out.println("CustomDefaultKeyboardFocusManager: dispatchKeyEvent");
      return false;
      }
      }


      class CustomKeyEventDispatcher2 extends KeyEventDispatcher {

      public boolean dispatchKeyEvent(KeyEvent ke) {
      System.out.println("CustomKeyEventDispatcher2: dispatchKeyEvent");
      // returns true so that the KeyboardFocusManager should take no further
      // action with regard to the KeyEvent
      //
      return true;
      }
      }

            naoto Naoto Sato
            rreynagasunw Rick Reynaga (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: