JDK-4460935 : JOptionPane focus problem
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-05-20
  • Updated: 2001-07-10
  • Resolved: 2001-07-10
Related Reports
Duplicate :  
Description

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) 
======================================================================

Comments
WORK AROUND Name: boT120536 Date: 05/20/2001 I have been messing around with this for quite awhile now and have not been able to make a work around. ======================================================================
11-06-2004

EVALUATION This is no longer reproducible on beta2 of merlin and is likely a result of focus work. This didn't work in meralin beta1 because of 4469073, which has been fixed for beta2. scott.violet@eng 2001-07-09
09-07-2001