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 How to write an event for 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 "How to write an event for "Enter" key?" Watch "How to write an event for "Enter" key?" New topic
Author

How to write an event for "Enter" key?

thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Hi everyone,
I have quite a simple problem i.e,i want to handle the "enter" key. For example,i enter text in a textfield and hit "enter",i want to send this to a textArea(the input)...am having a bit of trouble with keybindings...can someone please tell me how to start off with handling this?
Thanks in advance,
Thejaswini.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
If focus is on a JTextField and enter is pressed, the text field fires an ActionEvent. So to react to enter being pressed in your example:


There is no emoticon for what I am feeling!
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Thank you Jeff...
Its working now...however i also have a send button to do the task(same task) of retrieving input from textfield and sending it to a textarea...when i write listeners for both the button and the textfield,data is being logged twice.But i want data to be logged once whether the user presses enter or clicks the send button...Any tips for this?

Thanks,
Thejaswini
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I'm surprised that the text would be used twice since there are separate actions triggering each listener (enter versus a mouse click). Take a look at this demo I whipped up:

Having a default button for the window, by the way, allows the appending to fire when focus is on the text area and you press ENTER.
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Thank you Jeff.
Your code is working fine.I've been working on action events from two days and i found a problem with editable comboboxes.My application contains an editable combobox,a "SEND" button,a textarea for logging and a checkbox.When the checkbox is ticked, only response must be logged else,both commnad and response should be logged.Also the information is logged whether you hit enter/press "SEND".This is working fine as long as i dont tick the checkbox...but now i find tht the combobox fires an action event "comboBoxEdited" even when nothing is entered and my application does not work propoerly.This happens when i tick the check box as soon as i open the application.Can you help me with this?
Thanks for your time.

Thejaswini.
N Ganesh
Greenhorn

Joined: Jan 27, 2006
Posts: 5
Try out a key listener for the text field and an action listener for the button.

textfield = new JTextField();
textfield.addKeyListener(
new KeyAdapater()
{
public void keyReleased( KeyEvent e )
{
if( e.getKeyCode() == KeyEvent.VK_ENTER )
{
// Your job
}
}
}
);

button = new JButton();
button.addActionListener(
// Override the actionPerformed method and so on


N Ganesh
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Thanks Ganesh...but i think a common code would be better for this as both combobox(editable) and button are doing the same function...is there some way to map the enter key with send button??Infact am able to tht but the checkbox in my application does not work properly when i do tht.Any comments??
Michael Gofman
Greenhorn

Joined: Nov 03, 2010
Posts: 2
Jeff Albertson wrote:If focus is on a JTextField and enter is pressed, the text field fires an ActionEvent. So to react to enter being pressed in your example:

Thank you Jeff.
Adding the following to ease finding this page for others as I've ran into this and had a hard time figuring it out. Feeling kinda silly about not figuring it out on my own.
Listening to Enter on JButton
Mnemonic Enter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write an event for "Enter" key?
 
Similar Threads
Capturing "Enter Key" Value in Javascript
Regarding Action and KeyEvents
Regarding Listener
Enter Key Code
How to detect ENTER key from console app?