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

JFormattedTextField setText doesn't work properly for some validators

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.4.1
    • client-libs

      Name: gm110360 Date: 07/15/2002


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

      A DESCRIPTION OF THE PROBLEM :
      For Formatters / Validators that don't accept "" as legal input, setText on JFormattedTextField fails.

      The problem is that setText is trying to removeAll, then insert at start, however the intermediate removeAll is illegal.

      I have overwritten setText so that one can see what is happening, removing this setText makes no difference to the outcome.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. run code
      2. enter 1 in the top field (unformatted)
      3. press the button
      4. press the button again
      5. press the button again

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      expected: the bottom field to contain 1 after each time setText is called

      actual: it displays, 1 then 11, then 111, etc (each time prepending a 1 then converting to a byte)

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Frame1 extends JFrame {
        public Frame1() {
          NumberFormatter formatter = new NumberFormatter();
          formatter.setAllowsInvalid(false);
          formatter.setValueClass(Byte.class);
          final JTextField unformatted = new JTextField();
          final JFormattedTextField formatted = new JFormattedTextField(formatter) {
            public void setText(String t) {
              try {
                  Document doc = getDocument();
                  System.out.println("before " + getText() + " (" + doc.getLength() +
      ")");
                  doc.remove(0, doc.getLength());
                  System.out.println("during " + getText() + " (" + doc.getLength() +
      ")");
                  doc.insertString(0, t, null);
                  System.out.println("after " + getText() + " (" + doc.getLength() + ")");
              }
              catch (BadLocationException ex) {
                System.out.println("ex " + ex);
              }
            }
          };
          JButton button = new JButton("setText");
          button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              formatted.setText(unformatted.getText());
            }
          });
          getContentPane().add(unformatted, BorderLayout.NORTH);
          getContentPane().add(formatted, BorderLayout.SOUTH);
          getContentPane().add(button, BorderLayout.CENTER);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setSize(500,500);
          setVisible(true);
        }

        public static void main(String[] args) {
          new Frame1();
        }
      }
      ---------- END SOURCE ----------
      (Review ID: 159033)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: