Hi all.
I have problems problems restricting length of Japanese input
in JTextField. I use the following Document for restricting
the input length:
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class CheckLengthDocument extends PlainDocument {
private int numberOfChars = 0;
public CheckLengthDocument(int c) {
this.numberOfChars = c;
}
public void insertString(int offset, String str, AttributeSet
attributeSet)
throws BadLocationException {
int capacity = this.numberOfChars + 1 - getContent().length();
if (capacity >= str.length()) {
super.insertString(offset, str, attributeSet);
} else {
super.insertString(offset, str.substring(0, capacity),
attributeSet);
}
}
}
This document works fine with normal characters, but when entering
Japanese characters I can still enter characters when the maximum
length is reached and the newly entered characters overwrite the
previously entered characters. Of course this is not what I want.
How can I make this work correctly?
I am using JDK 1.3.1 on Windows2k/WindowsME. Switching to JDK
1.4/1.5 is not an option at the moment.
Christian
berlin123456 - 24 Sep 2004 21:18 GMT
Hello,
I have the same issue too. I searched all over, couldn't find any answer.
I "kind" solved the problem from the debugging data. Here is what I do,
inside the insertString method, when the AttributSet is not null, you want
the information to pass (do super.insertString). Only when the
insertString is null, do your logics. I have no fact to support what I am
doing and couldn't find any answer on the web. Hope this is helpful to
you. If you have found a different answer, please post and let me know.
Thanks.
Berlin