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

Problem using input masks in JFormattedTextField.

XMLWordPrintable



      Name: jk109818 Date: 01/10/2002


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

      Windows NT Version 4.0 SP6

      A DESCRIPTION OF THE PROBLEM :
      I am having problems with input masks on JFormattedTextField.

      I need
      to be able to format a date in a JFormattedTextField using the format
      "dd/MM/yyyy HH:mm:ss". I also want to restrict input so that the user can
      only enter characters that appear in the formatted date.

        Bug Id
      4467708
      (http://developer.java.sun.com/developer/bugParade/bugs/4467708.html)
      says that I can do this by using the following code

      new
      JFormattedTextField(new SimpleDateFormat("dd-MM-
      yyyy
      HH:mm:ss"));

      but this is not the case as this only causes the
      input to
      be validated once entered, it does not mask the input.

      I
      could use


      new JFormattedTextField(new
      MaskFormatter("##:##:## ##/##/####"));

      but this does not check
      that the input is a valid date.

      I have tried my
      code

      SimpleDateFormat formatDateTime = new
      SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
              


      JFormattedTextField field2 = new
      JFormattedTextField(formatDateTime);

      with both 1.4-beta and 1.4-
      beta3 and neither of them appear to work as the comments on bug 4467708
      suggest they should.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. With the sample source below select the top control
      (JFormattedTextField initialised with a SimpleDateFormat).
      2. type
      in werwerewtwert or another random sequence and the control accepts it
      but detects it is an invalid date when you tab off the field and reverts to
      the date initially set.
      3. Select the botton control
      (JFormattedTextField initialised with a MaskFormatter).
      4. Type in
      99-99-9999 99:99:99. As you cannot specify that this is a date the control
      accepts the text inputted.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Using the source code provided I would expect the JFormattedTextField to
      generate an input mask that matches the SimpleDateFormat passed in so
      that the user cannot enter an invalid date. This is my interpretation of
      what Bug Id 4467708 suggests should happen.

      What actually happens is
      that the user can type anything into the JFormattedTextField using a
      SimpleDateFormat and the validity of the value is checked when you tab off
      the field.

      You can use the MaskFormatter to provide the input mask and
      create a field that seperates day:month:year etc but you cannot tell the
      MaskFormatter to limit the range of a field to 0-59 for minutes etc, you can
      only tell it that a field is a numeric.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No errors occour

      This bug can be reproduced always.

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

      public class Application1
      {
          private
      boolean packFrame = false;

          /**Construct the application*/
          public Application1()
          {
              
      Frame2 frame = new Frame2();
              //Validate frames that have preset sizes
              //Pack frames that
      have useful preferred size info, e.g. from their layout
              if (packFrame)
              {
                  frame.pack();
              
      }
              else
              {
                  frame.validate();
              }
              //Center the window
              Dimension screenSize =
        Toolkit.getDefaultToolkit().getScreenSize();
              Dimension frameSize =
      frame.getSize();
              if (frameSize.height > screenSize.height)
              {
                  frameSize.height =
      screenSize.height;
              }
              if (frameSize.width > screenSize.width)
              {
                  frameSize.width =
      screenSize.width;
              }
              frame.setLocation((screenSize.width - frameSize.width) / 2,
      (screenSize.height - frameSize.height) / 2);
              frame.setVisible(true);
          }
          /**Main
      method*/
          public static void main(String[] args)
          {
              try
              {
                  
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              }
              
      catch(Exception e)
              {
                  e.printStackTrace();
              }
              new Application1();
          }
          
          class
      Frame2 extends JFrame
          {
              /**Construct the frame*/
              public Frame2()
              {
                  
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);

                  this.setTitle("JDK 1.4 Control Test");
                  
      JPanel contentPane = (JPanel) this.getContentPane();
                  
                  SimpleDateFormat formatDateTime
      = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

                  JFormattedTextField field1 = new
      JFormattedTextField(formatDateTime);
                  
                  field1.setValue(new Date());
                  
                  
      contentPane.add(field1, BorderLayout.NORTH);
                  
                  try
                  {
                      JFormattedTextField field2 =
      new JFormattedTextField(new MaskFormatter("##-##-#### ##:##:##"));
                      
                      
      contentPane.add(field2, BorderLayout.SOUTH);
                  }
                  catch(ParseException e)
                  {
                      //
      invalid mask characters
                  }

                  this.pack();
              }
          }
      }
      ---------- END SOURCE ----------
      (Review ID: 138137)
      ======================================================================

            peterz Peter Zhelezniakov
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: