Solved

Escape as a menu accelerator

Posted on 2002-04-29
6
325 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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 41
throw exception 21 66
Java syntax, or is it Selenium 6 37
Html Table Looping (part 2) 5 34
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

679 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