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

REGRESSION: dispatchEvent loses some KeyEvents >= JVM 1.4

XMLWordPrintable

    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      1.4.2_02

      ADDITIONAL OS VERSION INFORMATION :
      Windows 98 [Version 4.10.1998]
      Windows XP



      A DESCRIPTION OF THE PROBLEM :
      I created a keyboard menu to enter letters for an application. Using dispatchEvent for this worked fine in Personal Java, JVM 1.2.2, 1.3.1. however, in 1.4.1, 1.4.2, all keystrokes were lost. When I added a requestFocus for the control receiving the events, it still lost some events.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      in sample app provided, tap on the [A-Z] button
      [note: the two choice menus are related to a problem reported separately]

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      both "text" objects should display the letters A-Z in same sequence
      ACTUAL -
      1st text field displays all letters correctly.
      2nd text field displays all letters < JVM 1.4;
      but loses some of the letters >= JVM 1.4

      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;

      /*
      example that illustrates 2 problems with JVM >= 1.4 (at least on Windows)

      1. popup doesn't work for subclass of Choice
      2. keyevents lost

      these works fine in Personal Java, JVM 1.2.2, 1.3.1

      S. Weyer ###@###.###
      */

      public class ChoiceKeyTest
        extends Frame
        implements ActionListener
      {

        Choice choice1;
        ChoiceNT choice2;
        Button keyButton;
        KeyText text1, text2;
        char keyLetter;
        StringBuffer sb = null;

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

        public ChoiceKeyTest() {
          setTitle("ChoiceKeyTest; Java: " + System.getProperty("java.version"));
          addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent ev) {
                try { System.exit(0); }
                catch (IllegalStateException ignore) {}; // from Mac possibly
              }});

          choice1 = new Choice();
          choice1.add("Choice: popup ok");
          choice2 = new ChoiceNT();
          choice2.add("ChoiceNT: popup not ok JVM >= 1.4");
          String s;
          for (int i=1; i <= 5; i++) { // add a few more items
            s = Integer.toString(i);
            choice1.add(s);
            choice2.add(s);
          };
          keyButton = new Button("A-Z");
          keyButton.addActionListener(this);
          keyLetter = 'A';
          text1 = new KeyText("direct:");
          text2 = new KeyText("dispEv:");

          setLayout(new GridLayout(5,1));
          add(choice1);
          add(choice2);
          add(keyButton);
          add(text1);
          add(text2);
          pack();
          setVisible(true);
        }

        public void actionPerformed(ActionEvent ev) {
          if (ev.getSource() == keyButton) {
            // for text1, add 'key' directly
            text1.addChar(keyLetter);
            // for text2, add key via dispatchEvent
            text2.requestFocus(); // unnec for JVM < 1.4
            // even w/ requestFocus, some events still ignored/lost for >= JVM 1.4
            text2.dispatchEvent(new KeyEvent(this, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), KeyEvent.VK_SHIFT, keyLetter));
            keyLetter++;
            if (keyLetter > 'Z')
              keyLetter = 'A';
          }
        }
      }


      class ChoiceNT // for an app where another control uses Tab for internal navigation
        extends Choice
      {
        public boolean isFocusTraversable() { // refuse focus (via tab)
          return false;
        }
      }

      class KeyText
        extends Canvas
      {
        StringBuffer sb;
        static Font font = new Font("Monospaced", Font.PLAIN, 12);

        KeyText(String s) {
          addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ev) {
              addChar(ev.getKeyChar());
            }});
          sb = new StringBuffer(s);
        }

        void addChar(char ch) {
          sb.append(ch);
          repaint();
        }

        public void paint(Graphics g) {
          g.setFont(font);
          g.drawString(sb.toString(), 0, 14);
        }

        public final Dimension getMinimumSize() {
          return new Dimension(200,16);
        }
        public final Dimension getPreferredSize() {
          return getMinimumSize();
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I avoid using dispatchEvent (which is too bad since this seems like the 'right way' to make such controls work generally); I just call my internal "addChar" method directly.

      Release Regression From : 1.3.1
      The above release value was the last known release where this
      bug was known to work. Since then there has been a regression.
      ###@###.### 2005-03-28 22:17:08 GMT

            yan Yuri Nesterenko
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: