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

JFormattedTextField with NumberFormatter - Backspace not working properly

XMLWordPrintable



      Name: gm110360 Date: 05/01/2002


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

      FULL OPERATING SYSTEM VERSION : Windows 2000 Professional


      A DESCRIPTION OF THE PROBLEM :
      Using JFormattedTextField with a
      NumberFormatter/DecimalFormat.

      Everything works ok until you backspace over the decimal.
      When you do, the textfield will go from

      123.00 to 12300.00




      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the sample code
      2.
      3.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import javax.swing.text.MaskFormatter;
      import javax.swing.text.NumberFormatter;
      import java.awt.*;
      import java.text.DecimalFormat;

      public class TestCase {
          public static void main(String[] args) {
              JFrame frame = new JFrame("Formatted Text Field Demo");

              JLabel label3 = new JLabel("Decimal (3.14)");
              JLabel label4 = new JLabel("Integer (345)");

              JFormattedTextField tf3 = new JFormattedTextField();
              JFormattedTextField tf4 = new JFormattedTextField();

              JPanel panel = new JPanel();

              panel.setLayout(new GridLayout(0, 2));

              panel.add(label3);
              panel.add(tf3);
              panel.add(label4);
              panel.add(tf4);


              DecimalFormat decimalFormat = new DecimalFormat("#,###.00");
              decimalFormat.setMaximumFractionDigits(2);
              decimalFormat.setMinimumFractionDigits(2);
              decimalFormat.setMaximumIntegerDigits(4);
              decimalFormat.setMinimumIntegerDigits(0);
              NumberFormatter format3 = new NumberFormatter(decimalFormat);
              format3.setAllowsInvalid(false);
              format3.install(tf3);

              DecimalFormat integerFormat = new DecimalFormat("#,###");
              integerFormat.setMaximumIntegerDigits(4);
              integerFormat.setMinimumIntegerDigits(0);
              integerFormat.setMinimumFractionDigits(0);
              integerFormat.setMaximumFractionDigits(0);
              NumberFormatter format4 = new NumberFormatter(integerFormat);

              format4.setAllowsInvalid(false);
              format4.install(tf4);
              frame.getContentPane().add(panel, BorderLayout.CENTER);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.show();
          }

      }

      ---------- END SOURCE ----------
      (Review ID: 139699)
      ======================================================================

            peterz Peter Zhelezniakov
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: