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

JPanel cannot get focus even isFocusable() == true

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6
    • client-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      In the following code, although JPanel is focusable ( JPanel().isFocusable() == true), I have to call setFocusable() or the focus listener will not be called, why is it so strange ?


      class TestJPanel {
       
              public static void main(String[] args){
       
                      JFrame frame = new JFrame("test");
                      JPanel panel = new JPanel();
       
                      System.out.println("JPanel focusable ? " + panel.isFocusable());
       
                      // although JPanel is focusable, the below line is necessary
                      // or focus listener is not called, why ???
                      panel.setFocusable(true);
       
                      panel.addFocusListener(new FocusListener() {
       
                              public void focusGained(FocusEvent e) {
                                      System.out.println("focusGained " + e);
                              }
       
                              public void focusLost(FocusEvent e) {
                                      System.out.println("focusLost " + e);
                              }
                      });
       
                      frame.add(panel);
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
                      frame.pack();
                      frame.setBounds(100,100,400,300);
                      frame.setVisible(true);
              }
      }


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile the code, run it, click mouse button. the JPanel can get focus only when setFocusable is called.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      If isFocusable() is true, setFocusable(true) need not be called again.
      ACTUAL -
      setFocusable() must be called for JPanel to gain focus even JPanel.isFocusable() is true

      REPRODUCIBILITY :
      This bug can be reproduced always.

            son Oleg Sukhodolsky (Inactive)
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: