Hi,
Thank you for the valuable suggestion  it helped me write the extension.

Regards ,
Gene 

Restirct No of Lines Text Area Extension

import com.ulcjava.base.application.ULCTextArea;

public class ULCMyTextArea extends ULCTextArea {
    protected String typeString() {
        return "com.isfsdc.UIMyTextArea"; 
    }
}


import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.PlainDocument;


import com.ulcjava.base.application.ULCTextArea;
import com.ulcjava.base.client.UILabel;
import com.ulcjava.base.client.UITextArea;
import com.ulcjava.base.shared.internal.Anything;

public class UIMyTextArea extends UITextArea {
    AbstractDocument doc;
    static final int MAX_CHARACTERS=100;
    private JTextArea MyTextArea =  null ;
   
    public UIMyTextArea() {
        super();
    }
   
    protected Object createBasicObject(Anything args) {
        if (MyTextArea == null ) {
            MyTextArea = new  JTextArea(); 
            ((PlainDocument) MyTextArea.getDocument()).setDocumentFilter(new DocumentSizeFilter(MAX_CHARACTERS));
        }
        return MyTextArea;
    }
   
}


import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class DocumentSizeFilter extends DocumentFilter {
    int maxCharacters;
    boolean DEBUG = false;

    public DocumentSizeFilter(int maxChars) {
        maxCharacters = maxChars;
    }

    public void insertString(FilterBypass fb, int offs,
                             String str, AttributeSet a)
        throws BadLocationException {
        if (DEBUG) {
            System.out.println("in DocumentSizeFilter's insertString method");
        }

        //This rejects the entire insertion if it would make
        //the contents too long. Another option would be
        //to truncate the inserted string so the contents
        //would be exactly maxCharacters in length.
        if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
            super.insertString(fb, offs, str, a);
    }
   
    public void replace(FilterBypass fb, int offs,
                        int length,
                        String str, AttributeSet a)
        throws BadLocationException {
        if (DEBUG) {
            System.out.println("in DocumentSizeFilter's replace method");
        }
        //This rejects the entire replacement if it would make
        //the contents too long. Another option would be
        //to truncate the replacement string so the contents
        //would be exactly maxCharacters in length.
        if ((fb.getDocument().getLength() + str.length()
             - length) <= maxCharacters)
            super.replace(fb, offs, length, str, a);
    }

}


New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.

Reply via email to