JDK-4518307 : InputVerifier is caught in a infinite loop in 1.4beta2
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_nt,windows_2000
  • CPU: generic,x86
  • Submitted: 2001-10-23
  • Updated: 2002-06-24
  • Resolved: 2002-03-09
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.
Other
1.4.1 hopperFixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description

Name: ddT132432			Date: 10/23/2001


java version "1.4.0-beta2"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)

Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)

There is the class javax.swing.InputVerifier in the SDK 1.4. I used it in my
project under SDK 1.3 and it was OK. But now, under SDK 1.4 it works wrong.

I found two problems that together makes my program unusable.

The first one is that the method shouldYieldFocus(...) is called in the both
cases, when the mouse button is pressed and when it is released. Therefore,
this method is called when user enters any field by the mouse click.

The second one is that the method shouldYieldFocus(...) is called when I am
opening the JOptionPane with the message about error from the body of this
method. It results into the infinete loop. I think, that the field lost focus
when the JOptionPane is displayed.

The source code that demonstrates this problem follows (It is entire file with
the Win.java name). There is main method in this class, you can run it. Thre
are three fields. The first one is without input verifier, the rest ones have
setted input verifier. Click on the second field and you receive the infinite
loop (and messages from the body of shouldYieldFocus(...) method in the
console).

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

public class Win extends JFrame
{
  JTextField jTextField1 = new JTextField();
  JTextField jTextField2 = new JTextField();
  JTextField jTextField3 = new JTextField();

  public Win()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }
  public static void main(String[] args)
  {
    Win win = new Win();
  }
  private void jbInit() throws Exception
  {
    this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
    this.getContentPane().add(jTextField1);
    this.getContentPane().add(jTextField2);
    this.getContentPane().add(jTextField3);

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

    InputVerifier iv = new InputVerifier() {
      public boolean shouldYieldFocus(JComponent input) {
        System.out.println("shouldYieldFocus called");

        JOptionPane.showMessageDialog(null, "Something is wrong.");
        
        return false;
      }

      public boolean verify(JComponent input) {
        System.out.println("verify called");

        return false;
      }
    };

    jTextField2.setInputVerifier(iv);
    jTextField3.setInputVerifier(iv);

    this.pack();
    this.setVisible(true);
  }
}
(Review ID: 134295) 
======================================================================

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

PUBLIC COMMENTS .
10-06-2004

EVALUATION What you are doing is causing a recursive call. According to the documentation shouldYieldFocus checks whether the JComponent's input is valid. When displaying the option pane the input verifier gets run again. ###@###.### 2001-10-24 JComponent.requestFocus doesn't check to see if the component already has focus before running the input verifier. The second part is that DefaultCaret is calling request focus on both mousePressed and mouseClicked. ###@###.### 2001-10-25 Filing another bug regarding the issue with text fields running input verifiers on both mouse pressed and released (4532513). ###@###.### 2001-11-28 Filing another bug regarding shouldYieldFocus not allowing side effects (4532517). ###@###.### 2001-11-28
28-11-2001

SUGGESTED FIX *** /net/jano/export/disk09/swing/joutwate/tiger/input-verifier-4518307/webrev/src/share/classes/javax/swing/JComponent.java- Fri Nov 2 16:47:08 2001 --- JComponent.java Mon Oct 29 14:21:48 2001 *** 1076,1092 **** public boolean isRequestFocusEnabled() { return !getFlag(REQUEST_FOCUS_DISABLED); } private boolean runInputVerifier() { if (!getVerifyInputWhenFocusTarget()) { return true; } - Component focusOwner = - KeyboardFocusManager.getCurrentKeyboardFocusManager(). - getFocusOwner(); if (focusOwner == null || !(focusOwner instanceof JComponent)) { return true; } JComponent jFocusOwner = (JComponent)focusOwner; --- 1076,1097 ---- public boolean isRequestFocusEnabled() { return !getFlag(REQUEST_FOCUS_DISABLED); } private boolean runInputVerifier() { + Component focusOwner = + KeyboardFocusManager.getCurrentKeyboardFocusManager(). + getFocusOwner(); + + if (focusOwner == this) { + return true; + } + if (!getVerifyInputWhenFocusTarget()) { return true; } if (focusOwner == null || !(focusOwner instanceof JComponent)) { return true; } JComponent jFocusOwner = (JComponent)focusOwner; ###@###.### 2001-11-05
05-11-2001