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

JFormattedTextField does not use editformatter on initial focus with setText()

XMLWordPrintable

    • generic, x86
    • generic, windows_xp

      Name: gm110360 Date: 04/15/2004


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

      FULL OS VERSION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The edit formatter should always be used when the field is in focus.

      With value set using setText()/setDocument() -

      When a JFormattedTextField initially gains focus it should use the edit formatter, not the display formatter.
      On initial focus gain, the JFormattedTextField displays the edit value in the display formatter format, not the edit formatter format.

      (Use the example program - tab through the fields once, and then again to see my meaning).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See source code:
      Tab through the fields once, and then again. the first time through the date and time fields, on focus gain, the fields are displayed using the 'display formatter', the second time through, they use the 'edit formatter'.
      The edit formatter should always be used when the field is in focus.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      With value set using setText()/setDocument() -
      When a JFormattedTextField initially gains focus it should use the edit formatter, not the display formatter.
      ACTUAL -
      On initial focus gain, the JFormattedTextField displays the value to edit in the display formatter format, not the edit formatter format.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JFormattedTextField;
      import javax.swing.text.DefaultFormatterFactory;
      import java.text.SimpleDateFormat;
      import javax.swing.text.DateFormatter;
      import java.text.DateFormat;
      import java.util.Date;
      import javax.swing.JLabel;
      import javax.swing.JTextField;

      public class JFormattedTextProblem
      {
        public JFormattedTextProblem()
        {
        }
        public static void main(String[] args)
        {
          JFrame frame = new JFrame();
          JPanel panel = new JPanel();
          panel.add(new JLabel("Nothing: "));
          panel.add(new JTextField("Focus Starts Here."));

          DateFormat displayDate = DateFormat.getDateInstance(DateFormat.MEDIUM);
          DateFormat editDate = DateFormat.getDateInstance(DateFormat.SHORT);

          JFormattedTextField dateField = new JFormattedTextField();
          dateField.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(editDate),new DateFormatter(displayDate),new DateFormatter(editDate)));

          panel.add(new JLabel("Date: "));
          panel.add(dateField);


          DateFormat displayTime = DateFormat.getTimeInstance(DateFormat.MEDIUM);
          SimpleDateFormat editTime = new SimpleDateFormat("HH:mm");

          JFormattedTextField timeField = new JFormattedTextField();
          timeField.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(editTime),new DateFormatter(displayTime),new DateFormatter(editTime)));

          panel.add(new JLabel("Time: "));
          panel.add(timeField);

          dateField.setText(displayDate.format(new Date()));
          timeField.setText(displayTime.format(new Date()));

          frame.getContentPane().add(panel);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.show();

        }
      }
      ---------- END SOURCE ----------
      (Incident Review ID: 193795)
      ======================================================================
      Contribution by java.net member leouser:


      A DESCRIPTION OF THE FIX :
      BUG ID: 5032471 JFormattedTextField does not use editformatter on initial focus with setText()
      FILES AFFECTED: javax.swing.JFormattedTextField
       JDK VERSION
       jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin

      Discusion(embeded in test case as well):
      /**
       * BUG ID: 5032471 JFormattedTextField does not use editformatter on initial focus with setText()
       * My conception of this bug is that setText was not being treaded as an
       * edit. It certainly starts the editing up and leaves it hanging, to work
       * itself out after several focuses. The fix I have in place treats setText
       * as an edit. If the side effect of setText is that it is now editing and
       * the edit is valid, the edit is commited. This has the effect of getting
       * the state set up correctly for the first and subsequent focuses.
       *
       * Impact: any code built around the fact that setText was not treated as
       * an edit may break. Though I find it questionable that anything will treat
       * it as such.
       *
       * Testing Strategy:
       * Ive used pretty much the code documenting the problem in the BUG report.
       * To prove that the fix works, start up the program and tab through the
       * fields. Things should flip to their edited value upon first focus. To
       * see it not work, use an unpatched version.
       *
       * FILES AFFECTED: javax.swing.JFormattedTextField
       *
       * JDK VERSION
       * jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin
       *
       * test ran succesfully on a SUSE 7.3 Linux distribution
       *
       * Brian Harry
       * ###@###.###
       * Jan 19, 2006
       */

      UNIFIED DIFF:
      --- /home/nstuff/java6/jdk1.6.0/javax/swing/JFormattedTextField.java Thu Dec 15 02:17:35 2005
      +++ /home/javarefs/javax/swing/JFormattedTextField.java Thu Jan 19 18:42:31 2006
      @@ -689,6 +689,40 @@
               doc.addDocumentListener(documentListener);
           }
       
      + /**
      + * Sets the text of this <code>TextComponent</code>
      + * to the specified text. If the text is <code>null</code>
      + * or empty, has the effect of simply deleting the old text.
      + * When text has been inserted, the resulting caret location
      + * is determined by the implementation of the caret class.
      + * <p>
      + * This method is thread safe, although most Swing methods
      + * are not. Please see
      + * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
      + * to Use Threads</A> for more information.
      + *
      + * Note that text is not a bound property, so no <code>PropertyChangeEvent
      + * </code> is fired when it changes. To listen for changes to the text,
      + * use <code>DocumentListener</code>.
      + *
      + * Setting the text is treated as an edit followed with a call to commitEdit().
      + * @param t the new text to be set
      + * @see #getText
      + * @see DefaultCaret
      + * @beaninfo
      + * description: the text of this component
      + */
      + @Override
      + public void setText(String text){
      + super.setText(text);
      + if(isEdited() && isEditValid()){
      + try{
      + commitEdit();
      + }
      + catch(ParseException pe){}//do nothing, we assume this will never happen.
      + }
      + }
      +
           /*
            * See readObject and writeObject in JComponent for more
            * information about serialization in Swing.


      JUnit TESTCASE :
      import javax.swing.*;
      import javax.swing.text.*;
      import java.text.*;
      import java.util.Date;
      import junit.framework.TestCase;
      import junit.textui.TestRunner;

      /**
       * BUG ID: 5032471 JFormattedTextField does not use editformatter on initial focus with setText()
       * My conception of this bug is that setText was not being treaded as an
       * edit. It certainly starts the editing up and leaves it hanging, to work
       * itself out after several focuses. The fix I have in place treats setText
       * as an edit. If the side effect of setText is that it is now editing and
       * the edit is valid, the edit is commited. This has the effect of getting
       * the state set up correctly for the first and subsequent focuses.
       *
       * Impact: any code built around the fact that setText was not treated as
       * an edit may break. Though I find it questionable that anything will treat
       * it as such.
       *
       * Testing Strategy:
       * Ive used pretty much the code documenting the problem in the BUG report.
       * To prove that the fix works, start up the program and tab through the
       * fields. Things should flip to their edited value upon first focus. To
       * see it not work, use an unpatched version.
       *
       * FILES AFFECTED: javax.swing.JFormattedTextField
       *
       * JDK VERSION
       * jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin
       *
       * test ran succesfully on a SUSE 7.3 Linux distribution
       *
       * Brian Harry
       * ###@###.###
       * Jan 19, 2006
       */
      public class JFormattedTextProblem extends TestCase{


          public JFormattedTextProblem(String name){
      super(name);
          }

          public static void main(String ... args){
      TestCase tc = new JFormattedTextProblem("testJFField");
      TestRunner.run(tc);
          }

          public void testJFField(){
      SwingUtilities.invokeLater(new ShowGui());

          }

          public static class ShowGui implements Runnable{

           public void run(){
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      panel.add(new JLabel("Nothing: "));
      panel.add(new JTextField("Focus Starts Here."));

      DateFormat displayDate = DateFormat.getDateInstance(DateFormat.MEDIUM);
      DateFormat editDate = DateFormat.getDateInstance(DateFormat.SHORT);

      JFormattedTextField dateField = new JFormattedTextField();
      dateField.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(editDate),new DateFormatter(displayDate),new DateFormatter(editDate)));

      panel.add(new JLabel("Data: "));
      panel.add(dateField);

      DateFormat displayTime = DateFormat.getTimeInstance(DateFormat.MEDIUM);
      SimpleDateFormat editTime = new SimpleDateFormat("HH:mm");

      JFormattedTextField timeField = new JFormattedTextField();
      timeField.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(editTime),new DateFormatter(displayTime),new DateFormatter(editTime)));

      panel.add(new JLabel("Time: "));
      panel.add(timeField);
      dateField.setText(displayDate.format(new Date()));
      timeField.setText(displayTime.format(new Date()));

      frame.add(panel);
      frame.pack();
      frame.show();
      }



          }



      }


      FIX FOR BUG NUMBER:
      5032471

            Unassigned Unassigned
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: