<Swing Dev> [Accessibility]Focus unable to traverse in the menubar

Jing Lv lvjing at linux.vnet.ibm.com
Fri Sep 16 08:22:11 UTC 2011


Hello,

        I've found an accessibility issue. Here is the simple testcase:

public static void main() {
         JFrame jf=new JFrame("Hello");
         JMenuBar menubar = new JMenuBar();
         menubar.add(new JMenu("sample menu").add(new JMenuItem("hello")));
         jf.setSize(300,300);
         String[] comboEditableArray = { "Editable JCombobox 1", 
"Editable JCombobox 2", "Editable JCombobox 3", "Editable JCombobox 4", 
"Editable JCombobox 5" };
         JComboBox jc=new JComboBox(comboEditableArray);
         jc.setEditable(true);
         JButton jb1=new JButton("ok");
         JButton jb2=new JButton("ok");
         JTree jTree;
         jTree = new JTree();
         jTree.setName("JTree");
         jTree.setFocusable(true);
         jTree.setSelectionInterval(0, 0);

         JPanel jp=new JPanel();
         jp.add(jb1);

         jp.add(jc);
         jp.add(jTree);
         jp.add(menubar);
         jp.add(jb2);

         jf.getContentPane().add(jp);
         jf.show();

         KeyboardFocusManager kFocusMan = 
KeyboardFocusManager.getCurrentKeyboardFocusManager();
         kFocusMan.setDefaultFocusTraversalPolicy(new 
ContainerOrderFocusTraversalPolicy());

         jf.setFocusTraversalPolicy(KeyboardFocusManager
         .getCurrentKeyboardFocusManager()
         .getDefaultFocusTraversalPolicy());

     }
     public static void main(String[] args) {

         //Schedule a job for the event-dispatching thread:
         //creating and showing this application's GUI
         javax.swing.SwingUtilities.invokeLater(
             new Runnable() {
                 public void run() { JEditableComboFocusTest.main(); }
             }
         );
     }

       Running this testcase on JDK6/7 will find if we set focus on 
"JTree" item and then press TAB key, the focus lost (which we except it 
should focus on the last "OK" button).
       It seems the problem is that, the focus stays in the menubar and 
does not move to the other component. The reason is the focus traversal 
keys are disabled in the JMenuBar. But since the JMenuBar is focusable, 
the focus is moving to JMenuBar and since the focus traversal keys are 
disabled, the focus is staying back there and not coming to other 
components. When the focus traversal keys are disabled.

       A simple fix would be that disable focus for JMenuBar. The patch 
may be:
diff --git a/src/share/classes/javax/swing/JMenuBar.java 
b/src/share/classes/javax/swing/JMenuBar.java
index 4f77268..e5db3f8 100644
--- a/src/share/classes/javax/swing/JMenuBar.java
+++ b/src/share/classes/javax/swing/JMenuBar.java
@@ -109,6 +109,8 @@ public class JMenuBar extends JComponent implements 
Accessible,MenuElement
      public JMenuBar() {
          super();
          setFocusTraversalKeysEnabled(false);
+        /* Changing focus to false by default as Menu bar should not 
get focus. */
+        setFocusable(false);
          setSelectionModel(new DefaultSingleSelectionModel());
          updateUI();
      }

       Any comments? Thanks.



More information about the swing-dev mailing list