JDK-4774166 : InputVerifier not called after a window loses then regains focus
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1,1.4.2,5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2002-11-05
  • Updated: 2004-10-13
  • Resolved: 2004-09-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 betaFixed
Related Reports
Relates :  
Description
Name: jk109818			Date: 11/05/2002


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]

ADDITIONAL OPERATING SYSTEMS :

Windows NT 4.0 (Service Pack 6)


A DESCRIPTION OF THE PROBLEM :
The InputVerifier for a JComponent is not called if the
window is deactivated and I click on another JComponent.
This activates the window and places the focus on the
second JComponent.

REGRESSION.  Last worked in version 1.3.1

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Set the focus on the first component.
2. Click on any other window to deactivate the java
   application.
3. Reactivate the window by directly clicking on the
   second component.

EXPECTED VERSUS ACTUAL BEHAVIOR :
The InputVerifier should be called to check the
contents of the first component. This in turn
should block the focus transfer if necessary.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestFrame
extends javax.swing.JFrame
{
    public TestFrame()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        setSize(400,100);
        setVisible(false);
        getContentPane().add(with_verifier);
        with_verifier.setBounds(96,12,300,24);
        getContentPane().add(another_field);
        another_field.setBounds(96,36,300,24);


        with_verifier.setInputVerifier(new InputVerifier() {
            public boolean verify(JComponent input) {
                System.out.println("verify called "+(count++));
                return false;
            }
            int count = 0;
        });
    }

    static public void main(String args[])
    {
        (new TestFrame()).setVisible(true);
    }

    javax.swing.JTextField with_verifier = new javax.swing.JTextField();
    javax.swing.JTextField another_field = new javax.swing.JTextField();
}
---------- END SOURCE ----------
(Review ID: 165980) 
======================================================================

Name: jk109818			Date: 08/22/2003


FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]


A DESCRIPTION OF THE PROBLEM :
We have an swing-application where we use the InputVerifier to validate and,
if validated ok, store the values in the database.
Our clients report that sometimes values are entered into fields, file saved, and when the file is reopened the fields are empty.
We isolated this to be a problem when our clients where copying data from a textfile and into our application.
They enter a value in a field, switches to another application and back again and entering/pasting a value in another field.

I think this must be corrected in some way, otherwise people would not trust the InputVerifier.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Lets say we have a swing-application with two textfields.

1.Enter some text in textfield A and leave textfield A focused.
2.Change to another application with Alt-Tab, so the other applications window gets focus and is partly or not covering our swing-application.
3.Use the mouse and click on textfield B in our swing-application while the other application has focus.
Then our swing-application and textfield B gets the focus.
Textfield A lost it's focus during this operation, but was not validated by its InputVerifier!

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Since textfield A had focus before our application lost it, I expected that
the Inputverifier of textfield A was called before textfield B received the focus when our swing-application retrieved the focus.
ACTUAL -
Textfield B gets the focus from textfield A and the textfield A's InputVerifier is not called. This leads to ,in our case, data loss and real confusion for our clients.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
//this is a modified version of the demo-class found in the InputVerifier's javadoc
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class VerifierTest extends JFrame
{
   public VerifierTest()
   {
      JTextField tf1 = new JTextField("Type \"pass\" here");
      getContentPane().add(tf1, BorderLayout.NORTH);
      tf1.setInputVerifier(new PassVerifier());

      JTextField tf2 = new JTextField("TextField2");
      getContentPane().add(tf2, BorderLayout.SOUTH);

      WindowListener l = new WindowAdapter()
      {
         public void windowClosing(WindowEvent e)
         {
            System.exit(0);
         }
      };
      addWindowListener(l);
   }

   class PassVerifier extends InputVerifier
   {
      public boolean verify(JComponent input)
      {
         JTextField tf = (JTextField) input;

         //<added by me to make it more visible>
         if ("pass".equals(tf.getText()))
            input.setBackground(Color.white); //and store value in databse.....
         else
            input.setBackground(Color.red);
         //</added by me to make it more visible>
         
         return "pass".equals(tf.getText());
      }
   }

   public static void main(String[] args)
   {
      Frame f = new VerifierTest();
      f.pack();
      f.setVisible(true);
   }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
//Add this to the constructor of the VerifierTest and the InputVerifier of
//textfield A is called when the window/swing-application looses it's focus.
      addWindowFocusListener(new WindowFocusListener()
      {
         public void windowGainedFocus(WindowEvent e)
         {
         }

         public void windowLostFocus(WindowEvent e)
         {

            Component c = VerifierTest.this.getMostRecentFocusOwner();
            if (c instanceof JComponent)
            {
               JComponent jc = (JComponent) c;
               InputVerifier iv = jc.getInputVerifier();
               if (iv != null) iv.verify(jc);
            }
         }
      });
(Review ID: 199880)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang FIXED IN: mustang INTEGRATED IN: mustang
14-09-2004

EVALUATION There are a number of focus related problems with internal frames that need to be investigated. ###@###.### 2002-11-06
06-11-2002