• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JTextComponent default key bindings ... big confusion!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Does anyone of you ever tried to replace the default behaviour of some "special" key bindings like: TAB, BACK_SPACE, SPACE, DELETE, ... from a JTextField?
I�m using jdk1.3 and I�m trying, like I've said, to create a custom JTextField which will have my special actions or responses for those KeyStrokes.
I thought it might be easy because you got the InputMap/ActionMap bindings. So, I create my custom bindings in the InputMap/ActionMap overriding the mentioned KeyStrokes. I checked that the only InputMap with actions in it was WHEN_FOCUSED so I worked with it. Surprisingly, it didn't work. I tried to override the key bindings of the associated Keymap but, since 1.3 wrapped Keymap with InputMap/ActionMap, the result was as meaningless as before.
Then, I tried with a KeyListener. My key listener couldn't be able to catch the TAB. The rest of KeyStrokes were caught but the default action kept showing, even if I explicitly consume the KeyEvent.
Also, I tried overriding the processKeyEvent method. Once again the default action was there.
I kept reading and I found the Swing's Input Method Framework and I got three times the confusion I had in the first place.
Could anyone give me a hint on this?
Thank you
Luis Alfredo
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A suggestion:
You could create a class that extends KeyAdapter. Override the three methods. In the methods, you could check if the inputs are the values you want; if they are not simply consume() the KeyEvent.
Add a KeyListener to the JTextField using the class you just made.
Sample:
.
.
JTextField tf = new JTextField();
addKeyListener(new CustomKey());
.
.
class CustomKey extends KeyAdapter
{
.
.
.
}
Hope that helps.
jacq
reply
    Bookmark Topic Watch Topic
  • New Topic