JDK-6430420 : Inconsistent JFormattedTextField behavior at FocusLost with Number value
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-05-25
  • Updated: 2011-02-16
  • Resolved: 2006-05-26
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
Java HotSpot Client VM 1.5.0_06-b05

ADDITIONAL OS VERSION INFORMATION :
Windows XP version 5.1

A DESCRIPTION OF THE PROBLEM :
Using a JFormattedTextField with Number , the behavior when focus is lost is not as expected. If you try to insert an invalid value (for example xx123), on the lost focus event the value remain (setting JFormattedTextField.COMMIT); other side, in you insert an invalid value in the following mask (123xx),  on the lost focus event the value change to 123.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Using the attached code, insert the text xx123 in the first input field, e try to change focus on the second input field. Do the same operation with tha value 123xx

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Each value should persist.
ACTUAL -
In the second case the value is truncated

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;

public class NewJFrame8 extends javax.swing.JFrame {
    
    public NewJFrame8() {
        initComponents();
    }
    
    public static JFormattedTextField createIntegerField(){
        
        NumberFormat numberFormat = NumberFormat.getInstance();
                 
        JFormattedTextField jFormattedTextField = new JFormattedTextField(numberFormat);
        jFormattedTextField.setFocusLostBehavior(JFormattedTextField.COMMIT);
        
        return jFormattedTextField;
    }
    
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextField1 = NewJFrame8.createIntegerField();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setText("jLabel1");
        jPanel1.add(jLabel1);

        jFormattedTextField1.setText("jFormattedTextField1");
        jPanel1.add(jFormattedTextField1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel2.setText("jLabel2");
        jPanel2.add(jLabel2);

        jTextField1.setText("jTextField1");
        jPanel2.add(jTextField1);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame8().setVisible(true);
            }
        });
    }
    
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    
}

---------- END SOURCE ----------

Comments
EVALUATION This is a bug of NumberFormat. I've filed a bug against i18n (6431200) to track this, refer to it for details.
26-05-2006