• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to track backspace key?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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);
txtCheck.setInputVerifier(inputVerifier);

KeyListener input = new KeyListener() {

public void keyTyped(KeyEvent e) {
if(!txtCheck.getInputVerifier().verify(txtCheck))
{
txtCheck.setText(txtCheck.getText().substring(0, inputVerifier.getTextLimit()));
java.awt.Toolkit.getDefaultToolkit().beep();
}

}

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

public void keyReleased(KeyEvent e) {
txtCheck.setText(txtCheck.getText());
}
};
txtCheck.addKeyListener(input);



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..?

Praveen
[ August 25, 2008: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Praveen Kumar Jayaram
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Praveen Kumar Jayaram
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob Prime.
I implemented the abstract methods of Document and found removing text is working.

Thanks a lot for your reply.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic