954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Key Press event Restriction

dear sir
Hi,

In text field I want to restrict No of character entered should be only 3 if fourth character entering in same TextFiled it should not allow.

How to solve it ?

Warm regards

sunil

gandiva_skg
Newbie Poster
1 post since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

Do you mean that the textfield should not allow a fourth char to be typed?

Or, that the String returned by getText() is rejected if over 3 chars?

szukalski
Posting Whiz in Training
295 posts since Jun 2004
Reputation Points: 21
Solved Threads: 1
 

Forgive my ignorance, I forget what the thread titles are when I'm working.
You want to restrict the textfield to only 3 chars right?
A possible solution would be to create a keyListener on the TextField and keep track of the textfield content. Do a check on keyPressed sort of like the following:

void keyPressed(keyEvent e) {
 numberOfCharsInTextField++;
 if (numberOfCharsInTextField > 3) {
  numberOfCharsInTextField--;
  textField.setText(oldText);
 }
}

Or something like that.

szukalski
Posting Whiz in Training
295 posts since Jun 2004
Reputation Points: 21
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You