how to invoking the JTextField with setting specified length
I am having the problem of using the JTextField by setting the length since envolving the key event..During that time i would like to use the state of editing the JTextField for specified length will be false and our specified remaining length of JTextField that editing will be true.May you help me to rectify this problem.please give some suitable example accordingly........
# 1
Do you mean you want to limit the lenght of the text field? If so the easiest way is to extend the PlainDocument class to restrict the input length, then use JTextField.setDocument to tell it to use your class.
To extend PlainDocument you override the insertString method to make sure the new text will fit,
/**
* Called when text is to be addd to the document. If the resulting text is within the
* max allowed the operation is allowed to continue. Otherwise only the amount of text up
* to the max is inserted.
*
* @param offset the offset within the document where the insertion should occur
* @param string the string to be inserted
* @param a the attribute set
* @throws BadLocationException if offset is outside of document
*/
public void insertString(int offset, String string, AttributeSet a) throws BadLocationException {
int allowed = max - getLength();
if (string == null || allowed == 0)
return;
if (allowed - string.length() < 0)
super.insertString(offset, string.substring(0, allowed), a);
else
super.insertString(offset, string, a);
}
Variable max
is simply an integer that is set using the constructor of the document object.
HTH