Solved

Escape as a menu accelerator

Posted on 2002-04-29
6
337 Views
Last Modified: 2007-12-19
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?
0
Comment
Question by:Aniruddha082500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:googlyralph
ID: 6977701
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... :-)
0
 
LVL 9

Accepted Solution

by:
Ovi earned 100 total points
ID: 6979611
Define two Action class and register there what you need. Afeter that create the buttons by sending them oh their constructor those two actions. Should work...

ex :

public class ExecuteAction extends AbstractAction {
  public ExecuteAction() {
    initAction();
  }

  protected void initAction() {
    //initialize the action fields with what you want
    putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
    putValue(Action.ACTION_COMMAND_KEY, "cmd");
    putValue(Action.LONG_DESCRIPTION, "descr");
    putValue(Action.MNEMONIC_KEY, "m");
    putValue(Action.NAME, "name");
    putValue(Action.SHORT_DESCRIPTION, descr);
    putValue(Action.SMALL_ICON, icon);
  }
 
  // called when action accurs
  public void actionPerformed(ActionEvent ae) {
  }
}
After that construct like JButton execute = new JButton(new ExecuteAction());

The same for stop
0
 

Author Comment

by:Aniruddha082500
ID: 6979678
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.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 9

Expert Comment

by:Ovi
ID: 6979825
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.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7992576
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
0
 

Expert Comment

by:SpideyMod
ID: 8048144
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question