Login | Register   
RSS Feed
Download our iPhone app
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
May 31, 2005



Application Security Testing: An Integral Part of DevOps

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
    //Creating an input map of the Root Pane
    InputMap map = updationPanel.getRootPane().getInputMap(
    //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)
    actionMap.put( "cancel", close);
    //Displaying the Panel
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.



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