Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write an event for "Enter" key?

 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
thejaswini ramesh
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic