Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text field actionlistener

Status
Not open for further replies.

jeremytaffy

Technical User
Sep 6, 2001
75
0
0
US
Is it possible to add an action listener to a text field? for example, i want to be able to type in the text field and hit "enter" and have it do something with the text field entry. i am trying to avoid making a jbutton that will clutter up my gui.

thanks ahead

j
 
you can implement a KeyListener:

Code:
class blah implements KeyListener

add it to your textbox

Code:
textBox.addKeyListener(this);

and these methods to your (or another) class

Code:
public void keyTyped(KeyEvent event) {
}

public void keyPressed(KeyEvent event) {
	if (event.getKeyCode() == KeyEvent.VK_ENTER || event.getKeyChar() == '\n') {
		//do whatever you want here
	}
}

public void keyReleased(KeyEvent event) {
}
David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top