File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to track backspace key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to track backspace key?" Watch "How to track backspace key?" New topic

How to track backspace key?

Praveen Kumar Jayaram
Ranch Hand

Joined: Jul 14, 2008
Posts: 56

I was trying to limit the no. of characters in a JTextField by using Inputverifier class.
I am able to acheive that. But now the problem is I can't use backspace, arrow keys, etc.
Since I am tracking key press event. I need to track backspace, arrow keys press too.
How to track the backspace, arrow keys press?

Here is my code:

public class JTextInputVerifier extends InputVerifier
int maxLength;
JTextField jtf;

public JTextInputVerifier(int maxLength) {
this.maxLength = maxLength;

public int getTextLimit()
return maxLength;
public boolean verify(JComponent c) {
jtf = (JTextField)c;
String str = jtf.getText();
if (str.length() >= maxLength)
return false;
return true;

final JTextInputVerifier inputVerifier = new JTextInputVerifier(10);

KeyListener input = new KeyListener() {

public void keyTyped(KeyEvent e) {
txtCheck.setText(txtCheck.getText().substring(0, inputVerifier.getTextLimit()));


public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");

public void keyReleased(KeyEvent e) {

Please suggest some ideas.
Is there any alternative way to restrict the no. of characters? which won't affect working of backspace, and other keys..?

[ August 25, 2008: Message edited by: Bear Bibeault ]

Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

You can wrap the text fields document in your own class that intercepts the insertString method:

Basically, all text components are backed by an implementation of javax.swing.text.Document. All edits in the text component (adding, removing, etc) are done to the document, and then shown in the text component.

You get that document, wrap it in the wrapper class, and put it back in the text component. This wrapper will make sure that adding something (can be a typed character, or a pasted string) will not cause the maximum length to be exceeded. If this would happen, the string will be cut off so it will cause the maximum length to be reached - but not exceeded.

There is one downside to this. If you use DocumentListeners and add those to "document" directly, that document will be exposed. My own implementation solves that by wrapping DocumentEvents as well, but you won't need that right now.

How To Ask Questions How To Answer Questions
Praveen Kumar Jayaram
Ranch Hand

Joined: Jul 14, 2008
Posts: 56
Thanks for your suggestion.
But after implementing the above mentioned idea. I am not able to delete the text from JTextField. Backspace/Delete key is not working.

what may be wrong?
Do I need to implement something else to support Backspace/Delete key??

Praveen Kumar Jayaram
Ranch Hand

Joined: Jul 14, 2008
Posts: 56
Thanks Rob Prime.
I implemented the abstract methods of Document and found removing text is working.

Thanks a lot for your reply.
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 878
Originally posted by Praveen Kumar Jayaram:

I was trying to limit the no. of characters in a JTextField by using Inputverifier class.

That's not what InputVerifier is for. You could use InputVerifier to
prevent the field from losing focus if it has too many characters,
but it won't actually prevent the user from typing in (or pasting
in) too many characters.

To prevent the user from typing too many characters at all, you could
try to use a KeyListener (as your code partially successfully does), or
you could use a custom Document as Mr. Prime describes, but IMHO
the easy way is to use a DocumentFilter.

DocumentFilter lengthFilter = new AudibleLengthFilter(10);
((AbstractDocument)txtCheck.getDocument()).setDocumentFilter( lengthFilter );

Get rid of the KeyListener stuff. It's fraught with peril.
[ August 25, 2008: Message edited by: Brian Cole ]

bitguru blog
I agree. Here's the link:
subject: How to track backspace key?
It's not a secret anymore!