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

Sort order traversal policy is skipping components in focus cycle

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      other
    • OS:
      generic

      Description

      OPERATING SYSTEM(S):
      --------------------
      All

      FULL JDK VERSION(S):
      -------------------
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b05)
      Java HotSpot(TM) Server VM (build 1.6.0_02-b05, mixed mode)


      DESCRIPTION:
      ------------

      Compile and run provided testcase.

      1) Use TABS to focus "next focusable component" or traverse components
         of application given below in forward direction.
         Result:forward focus cycle is missing few components displayed on
         the Panel.

      2) Use Shift+TABS to focus "Before focusable component" or traverse
         components of application given below in backward direction.
         Result:backward focus cycle is missing few components too displayed
         on the Panel.

      Notes:
       - Seen on all look and feel( WindowsLookAndFeel, GTKLookAndFeel,
         MotifLookAndFeel and MetalLookAndFeel) as well
       - Focus cycle consisting of focusable components should be common for
         both forward and backward traversal, which is not here.



      TESTCASE (SortOrderFocusProblem2.java):
      ---------------------------------------

      import javax.swing.*;
      import java.awt.event.*;
      import java.util.*;
      import java.awt.*;

      class SwingComponentsPanel extends JPanel {
          String[] comboArray = { "JCombobox 1", "JCombobox 2", "JCombobox 3", "JCombobox 4",
              "JCombobox 5"};

          String[] comboEditableArray = { "Editable JCombobox 1", "Editable JCombobox 2",
              "Editable JCombobox 3", "Editable JCombobox 4", "Editable JCombobox 5"};

          String[] listArray = { "JList 1", "JList 2", "JList 3"};

          static JButton jButton;

          JCheckBox jCheckBox;
          JComboBox jComboBox;
          JComboBox jComboBoxE;
          JEditorPane jEditorPane;
          JFormattedTextField jFormattedTextField;
          JList jList;
          JPasswordField jPasswordField;
          JRadioButton jRadioButton;
          JSpinner jSpinner;
          JTextArea jTextAreaNE;
          JTextArea jTextArea;
          JTable jTable;
          JTextField jTextField;
          JTextPane jTextPane;
          JToggleButton jToggleButton;
          JTree jTree;

          public SwingComponentsPanel() {
              this.setBorder(BorderFactory.createCompoundBorder(BorderFactory
                                                                .createTitledBorder("Swing Components"), BorderFactory
                                                                .createEmptyBorder(5, 5, 5, 5)));
              jButton = new JButton("JButton");
              jButton.setName("JButton");
              jButton.setFocusable(true);

              jCheckBox = new JCheckBox("JCheckBox");
              jCheckBox.setName("JCheckBox");
              jCheckBox.setFocusable(true);

              jComboBoxE = new JComboBox(comboEditableArray);
              jComboBoxE.setName("JEditable_ComboBox");
              jComboBoxE.setEditable(true);
              jComboBoxE.setFocusable(true);

              jComboBox = new JComboBox(comboArray);
              jComboBox.setName("JComboBox");
              jComboBox.setFocusable(true);

              jEditorPane = new JEditorPane("text/plain", "Editor Pane");
              jEditorPane.setName("JEditorPane");
              jEditorPane.setFocusable(true);

              jFormattedTextField = new JFormattedTextField("Formatted Text Field");
              jFormattedTextField.setName("JFormattedTextField");
              jFormattedTextField.setFocusable(true);

              jList = new JList(listArray);
              jList.setName("JList");
              jList.setFocusable(true);

              jPasswordField = new JPasswordField(10);
              jPasswordField.setName("JPasswordField");
              jPasswordField.setFocusable(true);

              jRadioButton = new JRadioButton("JRadioButton");
              jRadioButton.setName("JRadioButton");
              jRadioButton.setFocusable(true);

              jSpinner = new JSpinner(new SpinnerNumberModel());
              jSpinner.setName("JSpinnerNumber");
              jSpinner.setFocusable(true);

              jTextAreaNE = new JTextArea(6, 10);

              jTextAreaNE.setText("Non-Editable JTextArea");
              jTextAreaNE.setEditable(false);
              jTextAreaNE.setName("JNonEditableJTextArea");
              jTextAreaNE.setFocusable(true);

              jTextArea = new JTextArea(4, 8);
              jTextArea.setText("JTextArea");
              jTextArea.setName("JEditableJTextArea");
              jTextArea.setFocusable(true);

              jTable=new JTable(2,2);
              jTable.setFocusable(true);
              jTable.setName("JTextArea2");

              jTextField = new JTextField("JTextField");
              jTextField.setName("JTextField");
              jTextField.setFocusable(true);

              jTextPane = new JTextPane();
              jTextPane.setText("JTextPane");
              jTextPane.setName("JTextPane");
              jTextPane.setFocusable(true);

              jTree = new JTree();
              jTree.setName("JTree");
              jTree.setFocusable(true);
              //jTree.setEditable(true);

              // Adding all the components required
              add(jButton);
              add(jCheckBox);
              add(jComboBox);
              add(jTable);
              add(jTextArea);

              add(jComboBoxE);
              add(jEditorPane);
              add(jFormattedTextField);
              add(jList);
              add(jTextAreaNE);
              add(jPasswordField);
              add(jRadioButton);
              add(jSpinner);
              add(jTextField);
              add(jTextPane);
              add(jTree);
              this.setFocusable(false);
              this.setName("Swing Components");
          }
      }

      public class SortOrderFocusProblem2 extends JFrame {
          JMenuBar menuBar;
          JMenu dialogsMenu;
          JMenu internalFrameMenu;
          JMenu traversalMenu;
          JMenu zMenu;
          JMenuItem menuItem;
          JMenuItem dialogsMenuItem;
          JMenuItem internalTestMenuItem;
          JMenuItem zAWTMenuItem;
          JMenuItem zSwingMenuItem;
          JMenuItem zMixMenuItem;
          JMenuItem awtMenuItem;
          JMenuItem swingMenuItem;
          JMenuItem swawtMenuItem;

          public SortOrderFocusProblem2() {

              menuBar = new JMenuBar();
              setJMenuBar(menuBar);

              menuBar.setName(null); // will not be included in the
              // SortingFocusTraversalPolicy

              // Travesal Menu
              traversalMenu = new JMenu("Traversal");
              traversalMenu.setMnemonic(KeyEvent.VK_T);
              traversalMenu.setName("TraversalMenu");
              menuBar.add(traversalMenu);

              awtMenuItem = new JMenuItem("AWT Test", KeyEvent.VK_A);

              swingMenuItem = new JMenuItem("Swing test", KeyEvent.VK_S);
              swawtMenuItem = new JMenuItem("Both Swing and AWT test", KeyEvent.VK_S);
              traversalMenu.add(awtMenuItem);
              traversalMenu.add(swingMenuItem);
              traversalMenu.add(swawtMenuItem);

              // Dialogs Menu
              dialogsMenu = new JMenu("Dialogs");
              dialogsMenu.setMnemonic(KeyEvent.VK_D);
              dialogsMenu.setName("DialogsMenu");
              menuBar.add(dialogsMenu);

              dialogsMenuItem = new JMenuItem("Dialogs Test", KeyEvent.VK_I);
              dialogsMenu.add(dialogsMenuItem);

              // Z-order Menu
              zMenu = new JMenu("Z-Order");
              zMenu.setMnemonic(KeyEvent.VK_Z);
              zMenu.setName("ZOrderMenu");
              menuBar.add(zMenu);

              zAWTMenuItem = new JMenuItem("AWT Test", KeyEvent.VK_A);

              zSwingMenuItem = new JMenuItem("Swing Test", KeyEvent.VK_S);

              zMixMenuItem = new JMenuItem("AWT & Swing Mix Test", KeyEvent.VK_M);
              // add z-menuitems to Zmenu,
              zMenu.add(zAWTMenuItem);
              zMenu.add(zSwingMenuItem);
              zMenu.add(new JSeparator());
              zMenu.add(zMixMenuItem);

              // Internal Frame Menu
              internalFrameMenu = new JMenu("Internal Frame");
              internalFrameMenu.setMnemonic(KeyEvent.VK_I);
              internalFrameMenu.setName("InternalFrameMenu");
              menuBar.add(internalFrameMenu);

              internalTestMenuItem = new JMenuItem("Internal Frame Test", KeyEvent.VK_I);
              internalFrameMenu.add(internalTestMenuItem);

              JPanel jp = new SwingComponentsPanel();
              KeyboardFocusManager kFocusMan = KeyboardFocusManager.getCurrentKeyboardFocusManager();

              this.setFocusTraversalPolicy(
                 new SortingFocusTraversalPolicy(
                    new Comparator<Component>() {
                       public int compare(Component o1, Component o2) {

                           if (o1.getClass().toString().equals("class javax.swing.JPanel")
                               || o2.getClass().toString().equals("class javax.swing.JPanel")) {

                               return 1;
                           }

                           if (o1.getClass().toString().equals("class javax.swing.JLayeredPane")
                               || o2.getClass().toString().equals("class javax.swing.JLayeredPane")) {

                               return 1;
                           }

                           if (((Component) o1).getName() == null
                               && ((Component) o2).getName() != null) {

                               return 1;
                           } else if ((((Component) o1).getName() != null && ((Component) o2)
                               .getName() == null)) {

                               return -1;
                           } else if ((((Component) o1).getName() == null && ((Component) o2)
                               .getName() == null)) {

                               return 1;
                           } else {

                               return((Component) o2).getName().compareTo(
                                   ((Component) o1).getName());
                           }
                       }
                    }
                  )
                );
              
              //jp.setFocusCycleRoot(true);
              this.getContentPane().add(jp);
          }

          public static void main(String[] args) {
              SortOrderFocusProblem2 fr_sort = new SortOrderFocusProblem2();
              fr_sort.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              fr_sort.pack();
              fr_sort.setVisible(true);
          }

      }

        Attachments

          Activity

            People

            Assignee:
            dav Andrei Dmitriev (Inactive)
            Reporter:
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: