This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Key Event listener question.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic