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

Cannot set a document to JFormattedTextField got from JSpinner's editor

XMLWordPrintable

    • generic, x86
    • generic, windows_nt, windows_2000

      Name: jk109818 Date: 04/28/2003


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

      FULL OS VERSION :
      Windows NT Version 4.0

      A DESCRIPTION OF THE PROBLEM :
      My Application has a JSpinner. I got the textField from the editor of JSpinner

      ListEditor editor = ((JSpinner.ListEditor)spinner.getEditor())
      JFormattedTextField field = editor.getTextField();

      i want to restrict the user from entering more than 3 characters into JSpinner's textfield so created a document which limits to a maximum of 3 characters.
      i can also do this using MaskFormatter but i do not want to use it as it places an empty space when deleting a character which is not normal JTextField's behaviour.

      i set the document to the JSpinner's JformattedTextField

      field.setDocument(new LimitedDocument(3));

      but it doesnt work.it allows me to enter as many characters as possible in the field

      if i create an instance of JFormattedTextField and set the documnet to it it works as expected so it is a problem in setting document to JFormattedTextField got from JSpinner

      JFormattedTextField jfText = new JFormattedTextField();
      jfText.setDocument(new LimitedDocument(3); works fine



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1.create a JSpinner
      2.Get the editor of JSpinner
      3.Get the textField from the editor(JFormattedTextField)
      4.Create a document which allows maximum of 3 characters only
      5.set the document to the JFormattedTextField.
      6.Run the application
      7.Try to enter characters in the JSpinners TextField
      8.Notice that it allows as many characters as possible not just 3

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      Expected to hear a beep when user tries to enter more than 3 characters
      allowing any number of characters to be entered

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import java.beans.PropertyChangeEvent;
      import java.beans.PropertyChangeListener;
      import java.io.Serializable;
      import javax.swing.*;
      import javax.swing.JSpinner.ListEditor;
      import javax.swing.text.AttributeSet;
      import javax.swing.text.BadLocationException;
      import javax.swing.text.PlainDocument;
      import java.util.Vector;

      public class Frame1 extends JFrame
      {
         JPanel contentPane;
         private SpinnerListModel _blkNamemodel;
         BorderLayout borderLayout1 = new BorderLayout();
         public Frame1()
         {
            jbInit();
            setVisible(true);
         }
         public void InitBlkNameSpinList()
         {
            int j = 000;
            Vector _vBlockNameList = new Vector(256);

            String strBlockNumber = new String();
            for (int i = j ; i < 256 ; i++)
            {
              strBlockNumber = strBlockNumber.valueOf(i);
              String tempStr = "000" + strBlockNumber;
             final int START_OFFSET = strBlockNumber.length() ;
              strBlockNumber = tempStr.substring (START_OFFSET,tempStr.length());
              _vBlockNameList.addElement(strBlockNumber);
              _blkNamemodel = new SpinnerListModel(_vBlockNameList);
            }
         }
         private void jbInit()
         {
            contentPane = (JPanel) this.getContentPane();
            this.setSize(new Dimension(400, 300));
            this.setTitle("Frame Title");
            contentPane.setLayout(borderLayout1);
            InitBlkNameSpinList();
            JSpinner spinner = new JSpinner(_blkNamemodel);
            PlainDocument doc = new LimitedDocument(3);
            JFormattedTextField field = ((JSpinner.ListEditor)spinner.getEditor()).getTextField();
            field.setDocument(new LimitedDocument(3));
            
            JFormattedTextField filed = new JFormattedTextField();
            
            filed.setDocument(new LimitedDocument(3));
            contentPane.add(spinner,BorderLayout.NORTH);
            contentPane.add(filed,BorderLayout.SOUTH);

         }
         public static void main(String[] args)
         {
             new Frame1();
         }
         class LimitedDocument extends PlainDocument
         {
            private int _maxCharacters;
            public LimitedDocument(int maxChars)
      {
      _maxCharacters = maxChars;
      }
            public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException
            {
               if ((getLength() + str.length()) > _maxCharacters)
               {
                  Toolkit.getDefaultToolkit().beep();
                  return;
               }
               char[] source = str.toCharArray();
               char[] result = new char[source.length];
               int j = 0;
               for (int i = 0; i < result.length; i++)
               {
                  result[j++] = source[i];
               }
               super.insertString(offs, new String(result, 0, j), a);
            }
         }
      }
      ---------- END SOURCE ----------
      (Review ID: 184750)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: