Link to home
Start Free TrialLog in
Avatar of Aniruddha082500
Aniruddha082500

asked on

Escape as a menu accelerator

I have an MDI application that has JInternalFrames containing JTextArea objects. The app has a "Run" menu under which I have items like "Execute" and "Stop". When "Execute" option is selected, I run some simulation and when "Stop" option is selected, I stop the simulation. Now I want to add keyboard accelerators to these commands. I added Ctrl+E to "Execute" and it worked fine. I want to add "Escape" as the accelerator for "Stop" but it doesn't work. When no internal frame is open, escape is trapped properly and my action listener is invoked. But when an internal frame is open and the JTextArea inside this frame has the focus, the Escape key is not trapped. I think it is because of the JTextArea. How do I force the application to invoke the menu handler (i.e. the action listener for the Stop menu item) even when the JTextArea has the focus?
Avatar of googlyralph
googlyralph

Could you not assign a listener to the internal pane class whcih would envoke the appropriate menu item?

GR

(My swing is rather rusty though... :-)
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aniruddha082500

ASKER

Hi googlyralph and Ovi,

Thanks for your suggestions. But I think what you are suggesting are work-arounds and not solutions to the problem. I have already implemented a work-around by writing a KeyListener and trapping the escape key. But what I want to know is that when keyboard short-cuts like Ctrl+E etc invoke the appropriate menu, why doesn't Escape
invoke the associated menu when the text area has focus? I don't think your suggestions answer this question.

Thanks anyways.
I believe you don't know exactly the sense of workaround term. Why did you think the swing team from sun has made constructors for JMenuItem, JCheckBoxMenuItem, JButton, JCheckBox and so on to accept Actions ? For allowing usage of my actions? This implementation is a preffered one instead of hardcoding and mixing usefull code with design code. Please read the Command pattern, API docs, and tutorial for related information about Action and AbstractAction and you will see that is not in any case a workarround solution.

Good luck and happy coding.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: Ovi
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange