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

Traversal in dialog box is not appropriate

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 6u3
    • client-libs
    • x86, sparc
    • solaris_2.5.1, windows_xp

    Description

      OPERATING SYSTEM(S):
      Windows
      Linux

      DESCRIPTION:

      The testcase is designed with the default traversal policy set to Sorting which is applicable to the Swing components.Hence, the traversals in the dialog box use this policy.

      STEPS TO REPRODUCE:

      3. Compile and run the testcase below
      4. Click on Yes/No button
      5. Click ShowIT
      6. Now, use TAB to traverse through the options present

      Observed Result:
      The traversal is not smooth. For example, after Clicking the "Show It" button with "Yes/No" option , it doesn't move to the "No" option on the press of a TAB.
        
      Expected Result:
      It should traverse through the options on the press of a TAB.

      TESTCASE:

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

      public class focus2 extends JPanel {

          JFrame frame;
          Container c;
          JLabel label;

          public focus2() {

          frame = new JFrame("Dialog test");
          
          final int numButtons = 2;
          JRadioButton[] radioButtons = new JRadioButton[numButtons];
          final ButtonGroup group = new ButtonGroup();

          JButton showItButton = null;
          final String defaultMessageCommand = "default";
          final String yesNoCommand = "yesno";
          
          radioButtons[0] = new JRadioButton("OK (in the L&F's words)");
          radioButtons[0].setActionCommand(defaultMessageCommand);
          radioButtons[0].setMnemonic(KeyEvent.VK_O);

          radioButtons[1] = new JRadioButton("Yes/No (in the L&F's words)");
          radioButtons[1].setActionCommand(yesNoCommand);
          radioButtons[1].setMnemonic(KeyEvent.VK_Y);
          
          label = new JLabel("Click the \"Show it!\" button" + " to bring up the selected dialog.", JLabel.CENTER);



          for (int i = 0; i < numButtons; i++) {
          group.add(radioButtons[i]);
          }

         radioButtons[0].setSelected(true);

         showItButton = new JButton("Show it!");
         showItButton.setMnemonic(KeyEvent.VK_H);
         showItButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
         String command = group.getSelection().getActionCommand();

         // ok dialog
         if (command == defaultMessageCommand) {
         JOptionPane.showMessageDialog(frame,"Eggs aren't supposed to be green.");

         // yes/no dialog
          } else if (command == yesNoCommand) {
              int n = JOptionPane.showConfirmDialog(frame,"Would you like green eggs and ham?",
                 "An Inane Question", JOptionPane.YES_NO_OPTION);
      if (n == JOptionPane.YES_OPTION) {
              setLabel("Ewww!");
      } else if (n == JOptionPane.NO_OPTION) {
      setLabel("Me neither!");
      } else {
      setLabel("Come on -- tell me!");
             }
          }
         return;
        }
       });

        int numChoices = radioButtons.length;
        JPanel box = new JPanel();
        JLabel label1 = new JLabel("Some simple message dialogs");

        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
        box.add(label1);

        for (int i = 0; i < numChoices; i++) {
            box.add(radioButtons[i]);
        }

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(box, BorderLayout.NORTH);
        pane.add(showItButton, BorderLayout.SOUTH);

        MyJTabbedPane tabbedPane = new MyJTabbedPane();
        tabbedPane.addTab("Simple Modal Dialogs", null, pane,
      "Some simple message dialogs", KeyEvent.VK_S, 0);




        KeyboardFocusManager kFocusMan = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        kFocusMan.setDefaultFocusTraversalPolicy(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()); }
                    }

        }));

        c=frame.getContentPane();
        c.add(tabbedPane);
        c.add(label, BorderLayout.SOUTH);

        label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        frame.addWindowListener(new WindowAdapter()
          { public void windowClosing(WindowEvent e) {System.exit(0);
      }});
        frame.pack();
        frame.setVisible(true);
          

        }
        
        void setLabel(String newText) { label.setText(newText);
       }

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


      class MyJTabbedPane extends JTabbedPane {
      public void addTab(String title, Icon icon, Component component,
      String tip, int mnemonic, int index) {
      this.add(component, index);
      this.setTitleAt(index, title);
      this.setMnemonicAt(index, mnemonic);
      this.setToolTipTextAt(index, tip);
      }
      }

      Attachments

        Activity

          People

            son Oleg Sukhodolsky (Inactive)
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: