We try to make an extended TextArea widget to limit the number of characters
that can be entered by the user.
The client side code of the widget follows below.
When we start our application, the constructor and the setter for the maximum
length are correctly called, as in the console window we get:
XBasicTextArea constructor!
setMaxLength : 20
But none of the two methods ("replace" and "insertString") are being called in
our filter, whatever we type into the text area. The definitions of both
methods were literally copied from the java API documentation.
Somebody maybe has a suggestion to help us out?
Thanks and kind regards,
Alberto A. Smulders
HostDat Lda. - Portugal
package pt.hostdat.hcc.common.util.component.xtextarea.client;
import java.awt.Toolkit;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import com.ulcjava.base.client.UITextArea;
/**
* Client-side code of extended TextArea component
*
* @author Alberto Smulders
*/
public class UIXTextArea extends UITextArea {
protected Object createBasicObject(Object[] a) {
return new XBasicTextArea();
}
public class XBasicTextArea extends BasicTextArea {
private int fMaxLength = 0;
public int getMaxLength() {
return fMaxLength;
}
public void setMaxLength(int maxLength) {
System.out.println("setMaxLength : " + maxLength);
if (maxLength >= 0) {
fMaxLength = maxLength;
}
}
public XBasicTextArea() {
System.out.println("XBasicTextArea constructor!");
AbstractDocument document =
(AbstractDocument)getDocument();
document.setDocumentFilter(new DocumentSizeFilter());
}
class DocumentSizeFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb,
int offset,
String string,
AttributeSet attr)
throws BadLocationException {
System.out.println("insertString");
replace(fb, offset, 0, string, attr);
}
public void replace(DocumentFilter.FilterBypass fb,
int offset,
int length,
String text,
AttributeSet attrs)
throws BadLocationException {
System.out.println("replace");
int newLength = fb.getDocument().getLength() -
length + text.length();
if (fMaxLength == 0 || newLength <= fMaxLength)
{
fb.replace(offset, length, text, attrs);
}
else {
Toolkit.getDefaultToolkit().beep();
}
}
}
}
}
--
Echte DSL-Flatrate dauerhaft für 0,- Euro*!
"Feel free" mit GMX DSL! http://www.gmx.net/de/go/dsl
_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer