aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to know which key is pressed from keyboard using ActionListener or KeyListener? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to know which key is pressed from keyboard using ActionListener or KeyListener?" Watch "How to know which key is pressed from keyboard using ActionListener or KeyListener?" New topic
Author

How to know which key is pressed from keyboard using ActionListener or KeyListener?

Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
This is code in which i want to set text to text area from text field whenever "send button" or "Enter" is pressed on keyboard...I got by using button but how it is done using "enter key"


Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
Welcome to the Ranch.

Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
thanks for suggestion...
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
A simple solution is to specify the JRootPane's default button. You can get the root pane from the JFrame using getRootPane() and set the default button using setDefaultButton(myButton).
I'm not sure what will happen when the text components have focus though as they may consume the enter key press.
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
I got using "button" but how to set text to text area from textfield when "enter" key is pressed???
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
To quote the API docs:
"The default button is the button which will be activated when a UI-defined activation event (typically the Enter key) occurs in the root pane regardless of whether or not the button has keyboard focus (unless there is another component within the root pane which consumes the activation event, such as a JTextPane)."
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
I cant get you but when i press "enter" from keyboard i want the text from "textfiled" to be set to "textarea".
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
Your send button already does this when you click on it, so you register your send button as the root pane's default button and when you press 'Enter' on the keyboard the send button will be activated ie it will be as if you clicked it.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
BTW I've just looked at your code and noticed you are creating a new JFrame() on line 13. This line is not required as your class already extends JFrame.
Also you should move the calls to setSize and setVisible() to the end of the method ie after you have added all the components or the components don't display without resizing the frame.
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
Is it the right way??


Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
Is it the right way??

Does it compile?
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
getting error as Illegal start of expresion at j.setDefaultButton(JButton b1);
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
And why do you think that is? Look at what you are passing to the method and compare it to other method calls you make where you pass an object reference.
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
suggest me where to modify plzzz

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
You have now fixed the 2 lines of code but why have you moved them from where you had them to inside the actionPerformed() method?
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
running good but not performing actionlistener for "enter" key
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
It works for me. Show me your latest code.
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
For me, if I type some text into the text field and press the enter key, the text is transferred to the text area. Is that not what you want to happen?
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
really??? i'm not getting that dude
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
How you getting i dont know...are you using the same code pasted above??
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
I hadn't spotted that somewhere between your first post and the latest one you have added an ActionListener to the text field. That is causing the problem, remove line 31.
Pradeep Kumar Raju
Greenhorn

Joined: Mar 10, 2013
Posts: 20
Thanks a million friend for your valuable time

GOT IT!!!

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2285
    
  49
Thanks a million friend for your valuable time

My pleasure.
rohan sethi
Ranch Hand

Joined: Dec 14, 2012
Posts: 34

For the enter thing, try to learn this webpage
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to know which key is pressed from keyboard using ActionListener or KeyListener?