Forums Register Login

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

+Pie Number of slices to send: Send
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"


+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
thanks for suggestion...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I got using "button" but how to set text to text area from textfield when "enter" key is pressed???
+Pie Number of slices to send: Send
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)."
+Pie Number of slices to send: Send
I cant get you but when i press "enter" from keyboard i want the text from "textfiled" to be set to "textarea".
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Is it the right way??


+Pie Number of slices to send: Send
 

Is it the right way??


Does it compile?
+Pie Number of slices to send: Send
getting error as Illegal start of expresion at j.setDefaultButton(JButton b1);
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
suggest me where to modify plzzz

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
running good but not performing actionlistener for "enter" key
+Pie Number of slices to send: Send
It works for me. Show me your latest code.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
really??? i'm not getting that dude
+Pie Number of slices to send: Send
How you getting i dont know...are you using the same code pasted above??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks a million friend for your valuable time

GOT IT!!!

+Pie Number of slices to send: Send
 

Thanks a million friend for your valuable time


My pleasure.
+Pie Number of slices to send: Send
For the enter thing, try to learn this webpage
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4560 times.
Similar Threads
Problem with JRadioButton
Writing to file problems
Calculator Problem
Scrollbar for jtable
Problem while using prepared statement
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:18:48.