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

Component in a minimized Frame has focus and receives key events

XMLWordPrintable

    • b27
    • generic
    • generic
    • Verified

      This behavior is reproducable starting 5.0 on Linux and Solaris. It is not reproducable on Windows

      I've a FocusListener added to a button in a frame. When FOCUS_LOST is triggered for the button, I'm calling requestFocus on the button to return the focus to the button. Even if I minimize the frame, focus stays on the button and pressing enter key triggers ActionEvent for the button

      To reproduce:
      1. Run the code below
      2. When the Frames come up, notice that the focus is on the 'Click me' button.
      3. Minimize the Frame, see that FocusOwner is still the button on minimized Frame (see the console output)
      4. Press the space bar and it could be seen that action event is triggered for button (see the console output)

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

      public class Test {
          
          private Button b1, b2;
          
          public static void main(String[] args) {
              new Test();
              new Thread(new Runnable() {
                  public void run() {
                      while (true) {
                          System.out.println("Focus Owner: " +
                                             KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
                          try { Thread.sleep(5000); } catch (Exception e) {}
                      }
                  }
              }).start();
          }
          
          public Test() {
              Frame f = new Frame("F1");
              Frame f2 = new Frame("F2");
              f2.setLayout(new FlowLayout());
              f2.add(new Button("Sample"));
              f.setLayout(new FlowLayout());
              b1 = new Button("Click me");
              b1.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent event) {
                      System.out.println("Action performed");
                  }
              });
              b2 = new Button("Second Button");
              b1.addFocusListener(new FocusListener() {
                  public void focusGained(FocusEvent event) {
                      System.out.println("Button focus gained");
                  }
                  public void focusLost(FocusEvent event) {
                      System.out.println("Button focus lost");
                      b1.requestFocus();
                  }
              });
              f.add(b1);
              f.add(b2);
              f2.setLocation(0, 200);
              f2.setSize(150, 150);
              f2.setVisible(true);
              f.setSize(150, 150);
              f.setVisible(true);
          }
      }

            ant Anton Tarasov (Inactive)
            gramachasunw Girish Ramachandran (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: