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

JOptionPane focus problem

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs



      Name: boT120536 Date: 05/20/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      If you have a text component which has an ActionListener associated with it and
      the actionPerformed() method which handles the action pops up a OptionPane in a
      loop until the user correctly answers the question the second time through the
      loop both the original text component and the dialog will have the focus so
      when the user hits the [ENTER] key on the dialog it fires the actionPerformed
      again as opposed to dismissing the dialog.

      The following code assumes the user entered nothing in the textfield on the
      panel and then entered a number less than 1000 the first time the OptionPane is
      displayed.


      public class TestPanel extends JPanel implements ActionListener {

         private JTextField tf;

         public TestPanel()
         {
             super();
             tf = new JTextField();
             tf.setPreferredSize(new Dimension(100,20));
             tf.addActionListner(this);
             this.add(tf);
         }

         public void actionPerformed(ActionEvent e)
         {
            System.out.println("Action Performed");
            if(tf.getText().length() < 1) {
                Object obj = null;
                while(obj == null) {
                    String str = JOptionPane.showInputDialog(this, "Enter a
      Number", "Number Input", JOptionPane.QUESTION_MESSAGE);
                    System.out.println("SKU = " + str);
                    try {
                        obj = getItem(Long.parseLong(str));
                    } catch(Exception ex) {}
                }
                doSomethingWithObj(obj);
            }
         }

         private Object getItem(long sku)
         {
            if(sku < 1000) return null;
            return new Object();
         }

      }


      You will notice when you run this that if you enter nothing in the textfield
      and hit [ENTER] a dialog will popup. If you type 333 into the dialog and hit
      [ENTER] everything works properly and the dialog is dismissed but of course the
      logic will recognize that 333 is < 1000 so null will be returned and the dialog
      will be displayed again. When this happens notice that the textfield on the
      original panel now has the focus. When I type the dialog recieves the events
      and enters them into the dialog's textfield properly but when I hit [ENTER] the
      dialog does not dismiss rather the actionPerformed on the main panel's
      textfield gets called again which causes a second dialog to be displayed.

      Obviously this is a problem.
      (Review ID: 124288)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: