Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6256449

JFormattedTextField does not correctly update its Value property

XMLWordPrintable

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      JFormattedText field shows one value, while its Value property has different value.
      When focus is lost the commit action is COMMIT_OR_REVERSE.
      NumberFormat and NumberFormatter are used to limit the input to integer numbers only, with maximum of 10 digits.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the source code.
      Enter 12345678901111 and tab out of the text field, the field text changes to 5678901111, but its property Value remains 12345678901111.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Property Value should have value that matches the number shown in the text field - 5678901111
      ACTUAL -
      Property Value has value of 12345678901111

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

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

      public class Test1 {
      public static void main(String[] args) {
      JFrame f = new JFrame("Test");
      f.setSize(800, 400);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().setLayout(new BoxLayout(f.getContentPane(),BoxLayout.Y_AXIS));

      final NumberFormat df1 = DecimalFormat.getIntegerInstance();
      df1.setGroupingUsed(false);
      df1.setParseIntegerOnly(true);
      df1.setMaximumIntegerDigits(10);

      final NumberFormatter nf = new NumberFormatter(df1);
      nf.setValueClass(Long.class);

      final JFormattedTextField ftf = new MyFTF(nf);
      ftf.setColumns(20);

      ftf.addPropertyChangeListener("value",
      new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent e) {
      System.out.println("Property changed to " + ftf.getValue() +
      " text shows " + ftf.getText());
      }
      }
      );

      JPanel p = new JPanel();
      p.add(ftf);
      JButton b = new JButton("Value");
      p.add(b);
      f.getContentPane().add(p);
      f.show();
      }
      }

      class MyFTF extends JFormattedTextField {

      public MyFTF(NumberFormatter n) {
      super(n);
      }

      protected void processFocusEvent(FocusEvent e) {
      super.processFocusEvent(e);
      if (e.isTemporary()) return;
      if (e.getID() == FocusEvent.FOCUS_LOST) {
      System.out.println("In processFocusEvent: value is " +
      getValue() + " *** text value is: " + getText()
      );
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Instead of getValue(), call getText() and them format...() using the formatter associated with the JFormattedTextField component.
      ###@###.### 2005-04-18 08:51:56 GMT

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: