× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



> -----Original Message-----
> From: Geert Van Landeghem [SMTP:gvl@reynderseti.be]
> Sent: Friday, May 26, 2000 12:43 AM
> To:   JAVA400-L@MIDRANGE.COM
> Subject:      Document types
> 
> Hi,
> 
> Has anyone used or written subclasses of the PlainDocument class? I'm
> looking for Documenttypes
> that allow only alphanumeric input with limited length, a valid date and
> numeric input (with specified length and decimals)
> So I'm looking for a "TextDocument","DateDocument" and "NumericDocument"
> class. I would like to write these 
> classes myself but don't have the time...
> 
> Hasn't IBM released some beans with this specific behaviour? (comparable
> to
> DDS-field descriptions)
> 
> Any help would be appreciated
        [>]  

        Hello,

        Here is a simple class I whipped up as an example for
"TextDocument".  It allows you
        to set the maximum number of characters entered into a text field
and it will automatically
        force lower or upper case it desired.  The "NumericDocument" is
similar and should be 
        very simple.  You may want to consider having a "IntegerDocument"
and a "DecimalDocument".
        I have build a number of these for different projects.
"IntegerDocument" is the simplest since validation is
        the easiest (just accept a series of numbers).  

        With a "IntegerDocument" I usually have a maximum value and minimum
value property instead of a "maximum characters property" since
        this provides more control and gets one away from thinking of set
positions.

        Note:
        As with all entry validation in Swing avoid using keyevents.  We had
a number of contractors try this and its wrong!  You always control input
via the 
        Document.  Remember keyevents don't work if one uses cut and paste
or some non keyboard entry.  Also, remember that in MVC architecture values 
        are also coming from the model.

        Here is the code for a "TextDocument":

        --------------------------------------------------------------

        import java.awt.Toolkit;
        import javax.swing.text.*;

        /**
         * @author Timothy Sullivan
         * @version 1.0
         */
        public class TextDocument extends PlainDocument
        {
                // Special constant that allows unlimited character entry.
          public static int UNLIMITED = -1;
                
                // Constants that control case.
          public final static int UPPER_OR_LOWER = 0;
          public final static int UPPER = 1;
          public final static int LOWER = 2;

          private int fieldMaxCharacters = UNLIMITED;

          /**
            * TextDocument constructor comment.
            */
          public TextDocument()
          {
            super();
          }

          /**
           * @author Timothy Sullivan
           */
          public TextDocument(int maxCharacters)
          {
            this();
            setMaxCharacters(maxCharacters);
          }

          /**
             * TextDocument constructor comment.
             * @param c javax.swing.text.AbstractDocument.Content
                         * @author Timothy Sullivan
             */
          protected TextDocument(AbstractDocument.Content c)
          {
            super(c);
          }


          /**
             * Gets the textCase property (int) value.
             * @return The textCase property value.
             * @see #setTextCase
             * @author Timothy Sullivan
             */
          public int getTextCase()
          {
            return fieldTextCase;
          }
                
          /**
           * 
           */
          public void insertString(int offset, String s,
                                   AttributeSet attibuteSet) throws
BadLocationException
          {
            if (s == null)
              return;
            try
            {
              if (getMaxCharacters() != FormattedDocument.UNLIMITED)
              {
                if ((getLength() + s.length()) > getMaxCharacters())
                {
                  formatError();
                  return;
                }
              }
            }
            catch (Exception ex)
            {
              formatError();
              return;
            }

            switch (getTextCase())
            {
              case UPPER :
                super.insertString(offset, s.toUpperCase(), attibuteSet);
                break;
              case LOWER :
                super.insertString(offset, s.toLowerCase(), attibuteSet);
                break;
              default :
                super.insertString(offset, s, attibuteSet);
            }
          }

          /**
           * Sets the textCase property (int) value.
           * @param textCase The new value for the property.
           * @see #getTextCase
                 * @author Timothy Sullivan
           */
          public void setTextCase(int textCase)
          {
            fieldTextCase = textCase;
          }
          /**
           * @author Timothy Sullivan
           */
          protected void formatError()
          {
            //Toolkit.getDefaultToolkit().beep();
          }
          /**
           * @author Timothy Sullivan
           */
          public int getMaxCharacters()
          {
            return fieldMaxCharacters;
          }
          /**
           * @author Timothy Sullivan
           */
          protected boolean isValidLength(String s)
          {
            if (getMaxCharacters() == FormattedDocument.UNLIMITED)
              return true;

            if (s.length() > getMaxCharacters())
              return false;

            return true;
          }
          /**
           * @author Timothy Sullivan
           */
          public void setMaxCharacters(int maxCharacters)
          {
            fieldMaxCharacters = maxCharacters;
          }
        }

>   
+---
| This is the JAVA/400 Mailing List!
| To submit a new message, send your mail to JAVA400-L@midrange.com.
| To subscribe to this list send email to JAVA400-L-SUB@midrange.com.
| To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner: joe@zappie.net
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.