Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

How to make a JTextField ignore single-char keymaps

Thread view: 
mtnkodiak@gmail.com - 07 Jan 2005 21:53 GMT
Hello,

I work on an app that uses single-character app-wide keyboard
accelerators to trigger Actions.  Not ideal, but that's what the legacy
version of the app does, so we are honoring that.

We have a dockable panel that contains a JTextField, and when the user
types in that field, the actions that correspond to keys pressed are
fired.  We do not want this to happen, but are unable to figure out how
to stop it.  I scoured this and other groups, and the intarweb, with no
success.

Does anyone have any ideas as to how to make a JTextField ignore all
app-wide keymaps?

I tried code like this (_pickNameText is the name of the JTextField,
and "c" is one of the accelerator keys we're trying to ignore):

Action doNothing = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//do nothing
System.out.println( "hi" );
}
};
_pickNameText.getInputMap().put(KeyStroke.getKeyStroke("c"),
"doNothing");
_pickNameText.getActionMap().put("doNothing", doNothing);
_pickNameText.getActionMap().put("c", doNothing);

And I also tried code like this:

KeyStroke ksC = KeyStroke.getKeyStroke('c');
_pickNameText.getKeymap().removeKeyStrokeBinding( ksC );
_pickNameText.getKeymap().setResolveParent( null );

and all I ever got was blocking the input of the actual 'c' key, which
wasn't the intention.

Any help from you gurus out there would be most appreciated!
Thanks, --mtnK
Christian Kaufhold - 09 Jan 2005 15:27 GMT
> I work on an app that uses single-character app-wide keyboard
> accelerators to trigger Actions.  Not ideal, but that's what the legacy
[quoted text clipped - 11 lines]
> I tried code like this (_pickNameText is the name of the JTextField,
> and "c" is one of the accelerator keys we're trying to ignore):

It is unclear what you want to achieve. Do you want the keyboard
accelerators to work (and the character not to be inserted into the
text field), or vice-versa.

How are the accelerators implemented?


Christian
mtnkodiak@gmail.com - 10 Jan 2005 16:51 GMT
Thank you for your response.  Sorry for being unclear.

I want the characters to be inserted into the text box normally,
without triggering any previously-defined app-level actions to occur at
all.  Regular text-editing accelerators like ctrl-C and ctrl-V are
desireable, just not any additional accelerators defined in our app.

The accelerators are implemented in the corresponding Action
constructors when the actions are initialized.  These actions are
initialized and set up before the JTextField control in question is
even created.  As far as I'm concerned, the accelerators are standard
action mnemonics.
Christian Kaufhold - 11 Jan 2005 16:44 GMT
> I want the characters to be inserted into the text box normally,
> without triggering any previously-defined app-level actions to occur at
[quoted text clipped - 6 lines]
> even created.  As far as I'm concerned, the accelerators are standard
> action mnemonics.

Use "typed" key strokes for the actions.

Otherwise (if you used "pressed" key strokes), the pressed key strokes
will cause the action and the following "typed" key strokes will also
insert into the text field. There is nothing that can be done about it
as the pressed key stroke is processed even before (possibly) a type
key event follows.

Alternatively, map all "pressed" key strokes in the JTextField input
map to a disabled action.

Christian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.