File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTextComponent default key bindings ... big confusion! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextComponent default key bindings ... big confusion!" Watch "JTextComponent default key bindings ... big confusion!" New topic

JTextComponent default key bindings ... big confusion!

Luis Alfredo Perez

Joined: Mar 04, 2002
Posts: 7
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

bla bla
jacq carballo
Ranch Hand

Joined: Feb 10, 2002
Posts: 42
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.
JTextField tf = new JTextField();
addKeyListener(new CustomKey());
class CustomKey extends KeyAdapter
Hope that helps.
I agree. Here's the link:
subject: JTextComponent default key bindings ... big confusion!
It's not a secret anymore!