Solved

Escape as a menu accelerator

Posted on 2002-04-29
6
321 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 107
java jdbc batch example 8 41
String array comparison 4 38
Cisco ASA: Java web start no go, asdm launcher no go 3 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

838 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