• Post Reply Bookmark Topic Watch Topic
  • New Topic

disabling ENTER key

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way I can disable ENTER key in my Swing application? Another words, if user presses enter program does not anything.
thanks,
Alex
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A screwdriver, maybe?
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this is funny because?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry -- it's a slow day at work today, and the other Java forums are a bit slow. Here's my take on your problem though -- in an attempt to redeem my untimely humor.
In order to capture and ignore the enter key, you'd have to basically place listeners on all components that it could influence. With event chaining that's a bit harder (ActionMap usage).
What do you really mean when you say you want to capture the enter key? For starters, only one widget has focus when the enter key is pressed. If it doesn't care about the enter key, it will either not do anything, OR if event chaining is used, it will pass it to the parent ActionMap and the process repeats.
Now, the question is: what are you really trying to do? If there's a particular widget that normally does something with the enter key that you would suddenly like to ignore, you can decorate it (GoF pattern), but that's a bit of work. Extend JTextField, or whatever widget is giving you problems, throwing away all enter keys, but passing everything else to the super.
Other than that, I'm not sure there's any simple way (if at all) to do what you want to do. I don't believe there's any simple filter that you can place over the entire application.
Does that make up for my joke?
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess so...thanks for the info. Will try to do this...
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got me feeling all guilty now
Here's some more info -- my previous thought about a general application filter that all keystrokes go through was a bit premature. Apparently, the input method framework is designed just for that.
Use of the InputMethodEvent can consume the entry key events, effectively elminating them from reaching the various components in your application .... apparently. I'm not sure how it all ties together though. Apparently, this is mostly used for locale issues, but it looks like you could adapt it to screen out the enters.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class KeyboardFocusManager provides that sort of general filter much more easily:

The KeyEvent representing the push of Enter is consumed before being dispatched to any gui component.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!