You are absolutely right.

-----Original Message-----
From: Dave Wathen [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 27, 2002 12:22 PM
To: Reinstein, Lenny; 'baski'; Norell Peter; [EMAIL PROTECTED]
Subject: RE: Editing a JTable cell on double-click.


LimitDocument should be able to prevent the paste (this is why the document
is the right place to do this sort of thing and not key handlers).  In fact
it looks to me as if it won't even prevent me typing more than limit
characters in.  The test:

      if(offset < limit)

is checking whether I'm typing after the limit'th character.  If I fill the
text field with limit characters and then move to the beginning and start
typing again it won't stop me.  I would think the test should be:

        if ((getLength() + s.length()) < limit)

Dave Wathen
Canzonet Limited
http://www.canzonet.com
mailto:[EMAIL PROTECTED]

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Reinstein, Lenny
Sent: 27 March 2002 17:00
To: 'baski'; Norell Peter; '[EMAIL PROTECTED]'
Subject: RE: Editing a JTable cell on double-click.


You can write your own class that extends from Document or PlainDocument.
Ex.:

textField.setDocument(new LimitDocument(255)); // cannot type more than 255
chars into this text field

However, it will not prevent the users from pasting more text into this
component. You may have to intercept paste event and strip the data that is
to be copied in the component in that case, unless there's a simpler
solution for this problem that I am now aware of.

// ------------------
class LimitDocument extends PlainDocument
{
    private int limit;

    public LimitDocument(int limit)
    {
      super();
      setLimit(limit); // store the limit
    }

    public final int getLimit()
    {
      return limit;
    }

    /** intercept the string we want to enter into a text component */
    public void insertString(int offset, String s, AttributeSet
attributeSet)
        throws BadLocationException
    {
      // if we haven't reached the limit, insert the string
      if(offset < limit)
      {
        super.insertString(offset,s,attributeSet);
      } // otherwise, just lose the string
    }

    public final void setLimit(int newValue)
    {
      this.limit = newValue;
    }
}

-----Original Message-----
From: baski [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 27, 2002 6:11 AM
To: Norell Peter; Reinstein, Lenny; '[EMAIL PROTECTED]'
Subject: Re:Editing a JTable cell on double-click.


Hi..all..

can anyone tell me huw to set the text entry in a text
field..(for example, more than 6 characters shld not
be allowed to enter in that text field)..

thanx in advance
baskie



__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards�
http://movies.yahoo.com/
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to