Activating a Keystroke When Any Component in the Window Has Focus : Key Event « Event « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Event » Key EventScreenshots 
Activating a Keystroke When Any Component in the Window Has Focus
    

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class Main {
  public static void main(String[] argvthrows Exception {

    JButton component = new JButton();

    MyAction action = new MyAction();
    component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F2"),
        action.getValue(Action.NAME));
  }
}

class MyAction extends AbstractAction {
  public MyAction() {
    super("my action");
  }

  public void actionPerformed(ActionEvent e) {
    System.out.println("hi");

  }
}

   
    
    
    
  
Related examples in the same category
1. KeyListener, ActionListener Demo 1KeyListener, ActionListener Demo 1
2. KeyListener, ActionListener Demo 2KeyListener, ActionListener Demo 2
3. Key event on frameKey event on frame
4. Responding to KeystrokesResponding to Keystrokes
5. Key Event DemoKey Event Demo
6. Set java.awt.Container.getFocusTraversalKeys(int id)
7. KeyStroke.getKeyStroke("F2")
8. InputMap javax.swing.JComponent.getInputMap(int condition)
9. Activating a Keystroke When Any Child Component Has Focus
10. Listening to All Key Events Before Delivery to Focused Component
11. List keystrokes in the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT input map of the component
12. List keystrokes in the WHEN_IN_FOCUSED_WINDOW input map of the component
13. Converting a KeyStroke to a String
14. Creating a KeyStroke and Binding It to an Action
15. void InputMap.put(KeyStroke keyStroke, Object actionMapKey)
16. Map actions with keystrokes
17. Handling Key Presses
18. Get key pressed as a key character (which is a Unicode character)
19. Get key pressed as a key code
20. Get Key Text
21. KeyStroke to String
22. Construct a new key description from a given universal string descriptionConstruct a new key description from a given universal string description
23. Reads for modifiers and creates integer with required mask
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.