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

JFormattedTextField class allows invalid entry, has poor handling of dates

XMLWordPrintable

    • Fix Understood
    • x86
    • windows_xp

      Name: gm110360 Date: 03/11/2004


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

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

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Not Applicable

      A DESCRIPTION OF THE PROBLEM :
      The JFormattedTextField when used with a DateFormatter and a SimpleDateFormat gives a very poor user experience when typing dates. The usability of this class in this situation is all wrong.

      You can have a format of dd/MM/yyyy and enter a date of 10/12/2002 and then type the digit 2 to have a field containing 10/12/20022. Surely this is a bug.

      With 10/12/2002 in the field I cannot delete the date to leave " / / "

      An extract from some observations posted to one of the forums
      http://forum.java.sun.com/thread.jsp?forum=57&thread=495896

      A developer writing a program in any language should not have to worry about the little things that add extra cost to the development costs. If I add date handling functionality to a text field I would expect to have a field that behaves as a masked field that only allows syntactly correct dates to be entered. Sun is targetting enterprise wide applications that has poor too low level front end support that leaves much to be desired. For example how many developers across the globe have to code sorting and filtering in tables?

      There are also many other problems with the date validation and entry.
      In my example, enter a date of 22/02/2002 place the cursor at start of date and then type 3, they whole date changes to 04/03/2002.
      Select all characters in a date and try to overtype, you can't when field is in overtype mode.

      Change the example and comment out the line
      // df.setOverwriteMode(true);
      The field appears with eg a date of 25/02/2004
      place cursor at start of date and type a 1
      the date changes to 04/06/2004

      In attaching a DateFormatter to a field that has a method named setAllowsInvalid, I take that to mean that I can restrict entry to valid input. It wouldn't cost Sun much to get this right, yet the cost to the global community is large. Large to the extent that many companies are questioning the development of GUI's in swing.

      This is the functionality that I expect from an entry field for dates:
      1) Allow only syntactically correct input against a mask
      2) Allow the user to blank a field
      3) Determine if the syntactically correct date is valid
      4) To be able to fire an event as soon as a date becomes valid or invalid


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Place caret at the end of the date and then type any digit
      eg date in field is 10/12/2002


      Run the following code:
      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import javax.swing.*;
      import javax.swing.text.*;
      import java.util.*;
      import java.text.*;

      public class FormattedSample2
      {
         public static void main(String args[]) throws ParseException
         {
            JFrame f = new JFrame("JFormattedTextField Sample");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container content = f.getContentPane();
            content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            format.setLenient(false);

            DateFormatter df = new DateFormatter(format);
            df.setOverwriteMode(true);
            df.setAllowsInvalid(false);
            JFormattedTextField ftf1 = new JFormattedTextField(df);
            ftf1.setValue(new Date());
            content.add(ftf1);
            
            f.setSize(300, 100);
            f.setVisible(true);
         }
      }
      ---------- END SOURCE ----------


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      entering the 11th character into a date field that already contains 10 characters should reject the character.

      field should contain 10/12/2002
      ACTUAL -
      Field ends up with 10/12/20022

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Not applicable

      REPRODUCIBILITY :
      This bug can be reproduced always.
      (Incident Review ID: 242966)
      ======================================================================

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

              Created:
              Updated:
              Imported:
              Indexed: