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

Traversing focus backwards with only 1 focusable component loses focus

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.7.0_60"
      Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
      Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When there is only one focusable component in a focus traversal cycle, moving the focus backwards (shift+tab) causes focus to be lost until the mouse is used to click the component again.




      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a JFrame.
      2. Create a JPanel and add it to the JFrame.
      3. Create a JTextArea and add it to the JPanel.
      4. Pack the frame and set it visible.
      5. Ensure focus is in the text field.
      6. Tab forwards to confirm focus remains in the text field.
      7. Tab backwards.

      Focus will be lost from the text field and even tabbing forward will not return focus to the field.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expect that with 1 component in the focus cycle, tabbing forwards or backwards (shift+tab) should keep focus on that one component instead of losing focus.
      ACTUAL -
      When moving focus backwards (shift+tab) with only one focusable component in the focus cycle, focus is lost. Once lost, even moving focus forwards (tab) does not return focus to the component.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;

      public class Test {
      public static void main(String[] args) {
      final JFrame frame = new JFrame();
      final JPanel panel = new JPanel();
      final JTextField textField = new JTextField(10);
      frame.add(panel);
      panel.add(textField);
      frame.pack();
      frame.setVisible(true);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      public class CustomFocusTraversalPolicy extends ContainerOrderFocusTraversalPolicy {

      private static final long serialVersionUID = 1L;

      @Override
      public Component getComponentBefore(Container aContainer, final Component aComponent) {
      /*
      * This is a workaround for a swing bug in which a focus cycle with only 1 focusable
      * component loses focus when focus is shifted to the previous component.
      */
      final int focusableComponentCount = countFocusableChildren(aContainer);
      if (focusableComponentCount == 1) {
      SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
      aComponent.requestFocusInWindow();
      }
      });
      }
      return super.getComponentBefore(aContainer, aComponent);
      }

      private int countFocusableChildren(Component component) {
      int currentCount = 0;
      if (accept(component)) {
      currentCount++;
      }
      if (component instanceof JComponent) {
      final JComponent jComponent = (JComponent) component;
      for (final Component child : jComponent.getComponents()) {
      currentCount += countFocusableChildren(child);
      }
      }
      return currentCount;
      }
      }

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: