This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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.
 
jQuery in Action, 2nd edition
 
subject: Regarding Event handling for TextField when Tab Pressed
 
Similar Threads
JTabbedPane Problem
Problem with KeyEvent and VK_TAB
TextField validation
onchange and onsubmit event is called when enter key is pressed in Mozilla, ok in IE
JTable cell Reading