GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Regarding Event handling for TextField when Tab Pressed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Regarding Event handling for TextField when Tab Pressed" Watch "Regarding Event handling for TextField when Tab Pressed" New topic
Author

Regarding Event handling for TextField when Tab Pressed

satya sahu
Ranch Hand

Joined: Mar 26, 2003
Posts: 97
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

Joined: Mar 23, 2005
Posts: 12
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: May 06, 2004
Posts: 262
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.
 
 
subject: Regarding Event handling for TextField when Tab Pressed