• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Event handling for TextField when Tab Pressed

 
satya sahu
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a text field. I am doing some validation for that text field. If the user enters some invalid value and then presses tab, then appropriate message should be displayed. What event related to the text field i have to use so that i can catch the TAB pressed. If possible please give me some sample code.

Thanks in Advance
 
Ankit Pedro
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A KeyListener may be registered for key events with a TextField. When a key is pressed, the method keyPressed() of the KeyListener will be called with the KeyEvent as parameter.

Check this tutorial from sun.
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html


addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
// look for tab keys
if(event.getKeyCode() == KeyEvent.VK_TAB|| event.getKeyChar() == '\t') {
// Your code goes here.
}
}
else {
//Your code goes here. }
}
});
[ March 29, 2005: Message edited by: Ankit Pedro ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The InputVerifier class tries to make this kind of validation easy and its api has an example of how to use it.
 
Alan Moore
Ranch Hand
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're running JDK 1.4 or later, a KeyListener won't work; the new focus subsystem consumes TAB key events so keylisteners never see them. Definitely try the InputVerifier first.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic