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

Frame cannot handle keyboard events (keyTyped) if buttons are added to frame.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.2
    • 1.1.3, 1.4.0
    • client-libs
    • mantis
    • generic, x86, sparc
    • generic, solaris_2.5.1, windows_98, windows_2000



      Name: jtC48319 Date: 06/17/98


      Steps to recreate problem.

      This is a simple program that changes the
      backroung color of the frame. You have two
      options; yellow and blue.

      Press 'y' for yellow and 'b' for blue.
      Nothing happens. If you click the buttons with
      the mouse, everythings works as expected. It
      appears that the frame can never regain focus to
      accept key event(as defined in Core Java 1.1
      Foundations). I tried to get the focus for the
      frame by using the requestFocus() method, but it
      does nothing(which I noticed was already reported).

      NOTE: We are going through e-mail updates. If
      the provided e-mail is return, please send all
      future correspondence to ###@###.###.
      Thank you.
       
      Here is the code:

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

      public class ButtonTest extends Frame
      {

      public ButtonTest()
      {
        addWindowListener(new WindowAdapter()
        {
         public void windowClosing(WindowEvent e)
         {System.exit(0);}
        } );

        setSize(300, 200);
        setTitle("Button Test");
        setLayout(new FlowLayout());

        //Create button, add event handler, and add to frame
        Button yellow = new Button("Yellow");
        yellow.addActionListener(new ActionListener()
        {
         public void actionPerformed(ActionEvent e)
         {setBackground(Color.yellow);}
        } );
        add(yellow);

        //Create button, add event handler, and add to frame
        Button blue = new Button("Blue");
        blue.addActionListener(new ActionListener()
        {
         public void actionPerformed(ActionEvent e)
         {setBackground(Color.blue);}
        } );
        add(blue);

        //I can never pass through this callback,
        //the frame never seems to have focus
        addKeyListener(new KeyAdapter()
        {
         public void keyTyped(KeyEvent e)
         { ButtonTest.this.keyTyped(e); }
        } );

        //Tried using this method to gain focus for frame
        //but it does not work.
        requestFocus();
      }

      public void keyTyped(KeyEvent evt)
      {
        char keyChar = Character.toLowerCase(evt.getKeyChar());
        if (keyChar == 'y') setBackground(Color.yellow);
        if (keyChar == 'b') setBackground(Color.blue);
      }

      public static void main(String args[])
      {
        Frame f = new ButtonTest();
        f.show();
      }

      }
      (Review ID: 25346)
      ======================================================================

            son Oleg Sukhodolsky (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: