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

Focus owner is null for applications which have menus only.

XMLWordPrintable

    • beta2
    • generic, x86
    • generic, windows_nt, windows_2000

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

      This bug appears to have been introduced during the focus manager enhancements at build 35. It doesnt' appear in b34. This bug appears on both Windows and Solaris but I haven't done a lot of exhaustive testing on Solaris.

      Upon initialization of the following application, the focus owner appears to be null. If the button is added to the center of the content pane the the focus owner is correctly set to the button. I'm not sure what the exact circumstances is for setting the focus but in previous builds, the focus was set to one of the JMenus upon initialization.

      If you use "% java Bug true" to start up the test application then the timer will report the focus owner every 2 seconds.

      >>>> Bug.java
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      import javax.swing.border.*;

      public class Bug extends JFrame {
          private Timer timer;
          private JButton timerButton;

          public Bug() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Container content = getContentPane();
      content.setLayout(new BorderLayout());
      content.add("North", createMenuBar("1 - First Menu", true));
      content.add("South", createMenuBar("2 - Second Menu", false));

      ActionListener timerListener = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      reportFocusStatus();
      }
      };

      timer = new Timer(2000, timerListener);

      timerButton = new JButton("Start");
      timerButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      if (timer.isRunning()) {
      timer.stop();
      timerButton.setText("Start Timer");
      } else {
      timer.start();
      timerButton.setText("Stop Timer");
      }
      }
      });
      // content.add(timerButton, BorderLayout.CENTER);

      pack();
      show();
          }

          public void startTimer() {
      timer.start();
          }

          public void stopTimer() {
      timer.stop();
          }

          public JMenuBar createMenuBar(String str, boolean bFlag) {
      JMenuBar menubar = new JMenuBar();
      JMenuItem menuitem;

      JMenu menu = new JMenu(str);
      menu.setMnemonic(str.charAt(0));

      menubar.add(menu);
      for(int i = 0; i < 10; i ++) {
      menuitem = new JMenuItem("JMenuItem" + i);
      if(bFlag)
      menuitem.setMnemonic('0' + i);
      menu.add(menuitem);
      }
      return menubar;
          }

          // Focus diagnostic
          public void reportFocusStatus() {
      // KeyboardFocusManager fm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
      // Component comp = fm.getFocusOwner();
      Component comp = this.getFocusOwner();
      String className = "null";
      if (comp != null) {
      className = comp.getClass().getName();
      }
      System.out.println("FocusManager: getFocusOwner = " + className);
          }

          //
          // Use "true" as the first arg to start the focus status timer.
          public static void main(String argv[]) {
      Bug bug = new Bug();

      if (argv.length > 0 && argv[0].equals("true")) {
      bug.startTimer();
      }
          }
      }
      <<<< end Bug.java

            svioletsunw Scott Violet (Inactive)
            mdavidsosunw Mark Davidson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: