Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Extended JTextField Problem  RSS feed

 
Sriram Sankar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had Extended a JTextField Class, this is to restrict the user form entering more than the required length of value into the text field.
The problem i face is that my extended class works perfect when the user, types in the text field directly, but when he does some cut/CopyPaste activity, it accepts more then the required length.
I need to stop the user from pasting if it exceeds the required length of the corresponding field.
The code to check for the length is as given below
this.limit = columns;
addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent evt) {
if (getText().length() >= extJTextField.this.limit)
evt.consume();}});

here limit is the allowed lenght of the field entry.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sriram,
I would not use keyEvent to limit the length. Since you are working with JTextField try and use the Document stuff. The code segment below performs what you want and also handles the case of copy/paste.

Enjoy,
Manfred.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!