aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Key Event listener question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Key Event listener question." Watch "Key Event listener question." New topic
Author

Key Event listener question.

Matt De
Greenhorn

Joined: Jul 29, 2005
Posts: 5
Could someone explain to me how to make a Key Event Happen. I have done some button listeners for example.
usernameButton.addActionListener(new UsernameButtonListener());
Do I set up a key event the same way? If so what is the correct syntax when doing this?
I then have my actual even for the button listener as follows:
public class UsernameButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
try
{
username.setEditable(false);
outgoing.setEditable(true);
}//close try
catch(Exception ex)
{
ex.printStackTrace();
}// close catch
outgoing.setText("");
outgoing.requestFocus();
}// close actionPerformed
}// close UsernameButtonListener

That all works properly. I want to make a key event listener so when I press enter I can send text somewhere. I have looked and saw some stuff on Key Event and like virtual keyboard enter for the enter key and so forth. Could someone just give me a quick example of code to create a key event and make it so that when I press a key I fire off an event. Thanks this is my first attempt at a Key Event and I am still trying to get used to the Java API so I can figure out what the heck I need to do on my own lol, but I am not completely there yet.
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Take a look at this.
web page

Towards the bottom of the page they give examples of what I think you are trying to do.

hth.

Steve


Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Here is something else that may help.

I have a user interface that I've been tinkering with and after reading your post I decided it too could user a keylistener.

So in my Panel construtor I added the following code.



And added a KeyListener to my TextField


Then I added this class.


Does that help?
Matt De
Greenhorn

Joined: Jul 29, 2005
Posts: 5
Second reply was a great help it working just as I wanted now thanks for the help. I had something similar too, but not working... I need to get better at working with the API to get what I need. Thanks a bunch!
saager mhatre
Ranch Hand

Joined: Dec 20, 2000
Posts: 61
If you're just trying to trap the Enter key on a TextComponent, you could simply register an ActionListener to the component. The default input map for text components routes Enter strokes to the ActionListeners with the components text as the ActionCommand.
Matt De
Greenhorn

Joined: Jul 29, 2005
Posts: 5
saagar. I did get it working with the other example, but am just curious on your reply. Could you further explain or show an example? I do not complete understand what your saying, but I think I do. I am still new with events and stuff and Java as a whole so that probably explains my confusion and not anything that you said. Thanks
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
You are welcome!

I think saager my have been talking about something like this.


Please correct me if I am wrong saager.

Steve
(BTW; that code will delete or consume any key typed that is not a digit)

I'm still learning as well, so if saager has a better way, I'm interested in learning it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Key Event listener question.
 
Similar Threads
Which JButton is being pressed ? OK or Cancel ?
Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...
doing same job on two or more events
ENTER must activate the default button when pressed on a JTable
JTable problem