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

Recommended Answers

All 2 Replies

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.