JDK-4246149 : When creating a JOptionPane in a FocusListener, get two Dialogs
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.7,1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-06-13
  • Updated: 2000-10-23
  • Resolved: 2000-10-23
Related Reports
Duplicate :  
Description

Name: krT82822			Date: 06/12/99


[note:  this is similar to bug # 4195148, but am submitting separate one, since this seems to have slightly different issues]

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

/*
I am trapping a focusLost event and popping a Dialog in that method.
When I pop the Dialog, the focusLost method is getting called again,
which pops a second Dialog.

Here is the code:
*/

class LossDateListener extends FocusAdapter{
         
  public void focusGained(FocusEvent evt)
      {System.out.println("Focus Gained");}
                
  public void focusLost(FocusEvent evt){
                   
     System.out.println("Focus lost");
     String dateText = "blah dee blah";
     if(true)
         JOptionPane.showMessageDialog(new JFrame("blah"), "nebulous",
                 "Invalid",  JOptionPane.INFORMATION_MESSAGE );
       
   }
            
}

/*
I get two Focus lost messages and two Dialogs on top of each other.  
When I dismiss the dialog showing on top, the focus lost method
is called again which pops another Dialog.  So, I am trapped 
with unstoppable dialogs.

I am running this on WinNT4.0 service pack 4 and Win98
jdk1.1.7 or jdk1.2
jfc1.1.1 or the one with jdk1.2
*/


public class FocalPoint {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        Container c = f.getContentPane();
        c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
        JTextField tf1 = new JTextField();
                tf1.addFocusListener(new LossDateListener());
        JTextField tf2 = new JTextField();
        c.add(tf1);
        c.add(tf2);

        tf2.setNextFocusableComponent(tf1);

        f.pack();
        f.setVisible(true);
    }
}




-------------------- orig user description ----------


I am trapping a focusLost event and popping a Dialog in that method.
When I pop the Dialog, the focusLost method is getting called again,
which pops a second Dialog.

Here is the code:

class LossDateListener extends FocusAdapter{
         
  public void focusGained(FocusEvent evt)
      {System.out.printnl("Focus Gained");}
                
  public void focusLost(FocusEvent evt){
                   
     System.out.println("Focus lost");
     String dateText = lossText.getText();
     DateRules dateRules = new DateRules();
     String status = dateRules.validateLossDate(dateText);
     if(!status.equalsIgnoreCase("OK"))
         JOptionPane.showMessageDialog(mainFrame, status,
                 "Invalid",  JOptionPane.INFORMATION_MESSAGE );
       
   }
            
}

I get two Focus lost messages and two Dialogs on top of each other.  
When I dismiss the dialog showing on top, the focus lost method
is called again which pops another Dialog.  So, I am trapped 
with unstoppable dialogs.

I am running this on WinNT4.0 service pack 4 and Win98
jdk1.1.7 or jdk1.2
jfc1.1.1 or the one with jdk1.2
(Review ID: 55128) 
======================================================================

Name: krT82822			Date: 01/11/2000


(see also 4181306, 4195148 )

Classic VM (build JDK-1.2.2-001, native threads, nojit)

In the included code there is a JTextField and a JButton. The JTextField has a
focusListener. In this particular situation, the FocusListener.focusLost(...)
method creates a JOPtionPane. Normally, FocusListener.focusLost(...)
would only be called once when the focus on the JTextField is lost. When the
JOPtionPane is created, the focus for the JTextField has already been lost
(which is why the JOPtionPane got created in the first place). However,
FocusListener.focusLost(...) is getting called a second time, causing 2
JOPtionPanes to be created. FocusListener.focusGained(...) is not called before
FocusListener.focusLost(...) gets called for the second time. Therefore there
is no reason FocusListener.focusLost(...) should be called more than once.



JTextField textField = new JTextField("Tab out of me");
textField.addFocusListener( new FocusAdapter()
    {
        public void focusGained(FocusEvent e)
	{
	    System.out.println("FocusGained event.");
	}
			
	public void focusLost(FocusEvent e)
	{
	    System.out.println("\tFocusLost event.");
            String inputValue = JOptionPane.showInputDialog(new JFrame(),"This
is a JOPtionPane");
	}
    }
);
JFrame jf = new JFrame();
jf.getContentPane().setLayout(new FlowLayout());
jf.getContentPane().add(textField);
jf.getContentPane().add(new JButton("Button"));
jf.pack();
jf.setVisible(true);
(Review ID: 99283)
======================================================================

Comments
WORK AROUND Name: krT82822 Date: 06/12/99 none ====================================================================== Name: krT82822 Date: 01/11/2000 I suppose a counter could be used to keep track of how many times the focusLost method gets called, and if it's more then 1, reset counter to zero and return rather than showing the JOptionPane a second time (Review ID: 99283) ======================================================================
11-06-2004

EVALUATION Name: pzR10082 Date: 10/11/2000 This is not reproducible with Merlin focus API. ###@###.### 2000-10-11 ======================================================================
11-10-2000