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

REGRESSION: KeyListener and Focus Error

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs
    • generic, x86
    • generic, windows_2000



      Name: bsC130419 Date: 07/11/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)


        
      ---------------------------- code ----------------------------------
      /**
       * file name : listenerTest
       * JDK : JDK 1.3 and JDK 1.4
       */

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

      public class listenerTest extends JFrame implements KeyListener {
      private BorderLayout border = new BorderLayout();
      private JPanel contentPane;
      private JPanel titlePanel = new JPanel();
      private JPanel mainPanel = new JPanel();
      private JTextArea tArea = new JTextArea();
      private JButton exitButton = new JButton("exit");

      public listenerTest(){
      this.pack();

      try{
      thisInit();
      }catch(Exception ex){
      }

      this.addKeyListener(this);
      }

      protected void thisInit() throws Exception {
      contentPane = (JPanel)this.getContentPane();

      contentPane.setLayout(border);

      titlePanel.add(exitButton, null);

      tArea.setPreferredSize(new Dimension(150, 100));
      tArea.setEditable(false);

      mainPanel.add(tArea, null);

      contentPane.add(mainPanel, border.CENTER);
      contentPane.add(titlePanel, border.NORTH);

      this.requestFocus();
      }

      public void keyPressed(KeyEvent ev){
      tArea.append("' " + ev.getKeyChar() + " ' was Pressed \n");
      }

      public void keyReleased(KeyEvent ev){
                                              tArea.append("' " + ev.getKeyChar() + " ' was Released \n");
      }

      public void keyTyped(KeyEvent ev){
                                             tArea.append("' " + ev.getKeyChar() + " ' was Typed \n");
      }

      public static void main(String[] args){
      listenerTest listenTest = new listenerTest();
      listenTest.setSize(200, 200);
      listenTest.setVisible(true);
      listenTest.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent ev){
      System.exit(0);
      }
      });
      }
      }
      -----------------------------------------------------------------------

      also... in JDK 1.3 or JDK 1.3.1 it's OK...
      but JDK 1.4 key listener do not operate...

      ....... well... I'm Korean... so I'm poor at English...
      if you can read Korean... i will write a report in Korean...
      (Review ID: 127814)
      ======================================================================

      Name: rmT116609 Date: 11/16/2001


      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)

      Section 1: Steps
      ================
      (1) Run the following program with command "java KeyListenerBug"
      (2) Select anything in the dialog, so that the title bar is highlighted
      (3) Press any key on the keyboard

      What's wrong?
      In JDK1.3, the console prints out "KeyEvent detected" whenever a key is pressed.
      However, in JDK1.4 beta 3, the key event cannot be detected. They behave differently!

      I believe this is something to do with the new Focus architecture in JDK1.4. And I hope this can be fixed before it is released.

      Section 2: Source Code
      ======================
      import java.awt.event.*;

      public class KeyListenerBug extends JFrame
      {
          public KeyListenerBug()
          {
              super("Key Listener Bug");
             
              SwingUtilities.invokeLater(new Runnable()
              {
                  public void run()
                  {
                      MyDialog dialog = new MyDialog(KeyListenerBug.this);
                      dialog.setVisible(true);
                  }
              });

              setBounds(100, 100, 500, 500);
          }

          public static void main(String [] argv)
          {
              KeyListenerBug bug = new KeyListenerBug();
              bug.setVisible(true);
          }
          
          private class MyDialog extends JDialog
          {
              
              public MyDialog(JFrame frame)
              {
                  super(frame, "This is a dialog", true);
                  
                  JPanel main = new JPanel();
                  main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
                  main.add(new JLabel("This is a label"));
                  main.add(new JTextField("This is a test field"));
                  main.add(new JRadioButton("This is a radio button"));
                  
                  getContentPane().add(main);
                  
                  setBounds(200, 200, 150, 200);
                  
                  addKeyListener(new KeyAdapter()
                  {
                      public void keyPressed(KeyEvent e)
                      {
                          System.out.println("KeyEvent detected");
                      }
                  });
              }
          }
      }


      (Review ID: 135135)
      ======================================================================

            son Oleg Sukhodolsky (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: