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

Focus unable to traverse in the menubar

    XMLWordPrintable

Details

    • b78
    • generic
    • generic

    Description

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP SP2 32bit

      A DESCRIPTION OF THE PROBLEM :
      After focus moving into JMenuBar, whose focus traversal key is disabled by default, it never moves to other focusable component.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test case.
      sdk/jre/bin/java FocusTraversal

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Test finished successfully and silently.
      ACTUAL -
      Test failed with exception.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.Exception: Problem moving focus from javax.swing.JMenuBar
      at FocusTraversal.main(FocusTraversal.java:102)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Portions Copyright (c) 2011 xxxxx Corporation
       */

      import java.awt.Component;
      import java.awt.ContainerOrderFocusTraversalPolicy;
      import java.awt.BorderLayout;
      import java.awt.Robot;
      import java.awt.Toolkit;
      import java.awt.event.KeyEvent;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JPanel;
      import javax.swing.SwingUtilities;

      import sun.awt.SunToolkit;

      public class FocusTraversal {
          static JFrame jf = null;
          static Component owner = null;

          static final class SetupGui implements Runnable {
              public void run() {
                  jf = new JFrame("Focus traversal test");
                  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                  JMenuBar mb = new JMenuBar();
                  mb.add(new JMenu("menu1").add(new JMenuItem("menu_item1")));

                  JButton jb1 = new JButton("button1");
                  JButton jb2 = new JButton("button2");

                  JPanel jp = new JPanel(new BorderLayout());
                  jp.add(jb1);
                  jp.add(mb, BorderLayout.NORTH);
                  jp.add(jb2, BorderLayout.SOUTH);

                  jf.getContentPane().add(jp);

                  jf.setFocusTraversalPolicy(
                          new ContainerOrderFocusTraversalPolicy());

                  jf.pack();
                  jb1.requestFocusInWindow();
                  jf.setVisible(true);
              }
          }

          static final class GetFocusOwner implements Runnable {
              public void run() {
                  owner = jf.getFocusOwner();
              }
          }

          public static void main(String[] args) throws Exception {
              Robot robot = new Robot();
              robot.setAutoDelay(50);
              SunToolkit toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
              SwingUtilities.invokeAndWait(new SetupGui());
              try {
                  toolkit.realSync();

                  GetFocusOwner gfo = new GetFocusOwner();
                  Component last = null;
                  do {
                      SwingUtilities.invokeAndWait(gfo);
                      if (owner == last) {
                          throw new Exception("Problem moving focus from " +
                                  owner.getClass().getName());
                      }
                      last = owner;
                      // Press the TAB key to move the focus onto the next component
                      robot.keyPress(KeyEvent.VK_TAB);
                      robot.keyRelease(KeyEvent.VK_TAB);
                      toolkit.realSync();
                  } while (owner != jf);
              } finally {
                  jf.dispose();
              }
          }
      }

      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              rupashka Pavel Porvatov (Inactive)
              rupashka Pavel Porvatov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: