This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextComponent default key bindings ... big confusion!" Watch "JTextComponent default key bindings ... big confusion!" New topic
Author

JTextComponent default key bindings ... big confusion!

Luis Alfredo Perez
Greenhorn

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


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.
Sample:
.
.
JTextField tf = new JTextField();
addKeyListener(new CustomKey());
.
.
class CustomKey extends KeyAdapter
{
.
.
.
}
Hope that helps.
jacq
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextComponent default key bindings ... big confusion!
 
Similar Threads
NX: How to register a key event to a frame
OutOfMemory error with long JList using a CellRenderer
About Keybinding
getting KeyPressed for JDialog? example?
Swing Key bindings