dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Advanced
Jun 1, 2005

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Capturing the ESCAPE Key Event

Consider a scenario where a dialog is launched from a panel which itself was launched by another panel. It's very tedious to get the dialog disposed when the ESCAPE key is pressed. The code segment below makes it easy to capture the ESCAPE key event, irrespective of the hierarchy, and performs the designated action.

 //Has to be final because this instance is required in the inner class
    final UpdateCustomerData updationPanel = new UpdateCustomerData(mainPanel);
    //Setting the size
    updationPanel.setSize(400,150);
    //Creating an input map of the Root Pane
    InputMap map = updationPanel.getRootPane().getInputMap(
		JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
    //Adding the key that needs to be captured
    map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel" );
    ActionMap actionMap = updationPanel.getRootPane().getActionMap();
    //Creating the action that needs to be executed when the key is pressed
    Action close = new AbstractAction() {
	public void actionPerformed(ActionEvent ae)
	{
	    updationPanel.dispose();
	}
    };
    actionMap.put( "cancel", close);
    //Displaying the Panel
    updationPanel.setVisible(true);
updationPanel is the panel that will be affected by the ESCAPE key event here.
MS Sridhar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date