Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextComponent default key bindings ... big confusion!

 
Luis Alfredo Perez
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
jacq carballo
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic