Solved

Escape as a menu accelerator

Posted on 2002-04-29
6
307 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:Ovi
Comment Utility
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
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now