Results 1 to 3 of 3
  1. #1
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default Key Binding problem

    I need to execute some code whenever the user presses the F1 key irrespective of which sub-component of the main frame has current focus and decided to go with key binding to implement the above situation.

    For registering the keystroke F1 to the main frame of my swing application,
    I tried out the following code:

    Java Code:
    JFrame mainFrame = new JFrame("MyApp");        
    Action action = new AbstractAction() {
    	public void actionPerformed(ActiomEvent actionEvent) {
    		System.out.println("F1 pressed");
    	}
    };				
    
    String keyStrokeAndKey = "F1";				
    KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);				
    mainFrame.getRootPane().getInputMap().put(keyStroke, keyStrokeAndKey);				
    mainFrame.getRootPane().getActionMap().put(keyStrokeAndKey, action);
    Yet, on execution, the application does not respond to the F1 key.
    Is there something inherently wrong with the above code? Thanks.

  2. #2
    abc
    abc is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Not sure if this will help, but you can try adding a JPanel to the JFrame and adding the key stroke to JPanel instead.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JSP and javascript binding???
    By paulb in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-02-2011, 08:33 PM
  2. Help: Key Binding
    By mine0926 in forum New To Java
    Replies: 0
    Last Post: 08-06-2010, 04:52 AM
  3. Key Binding problem
    By dilpreet28 in forum New To Java
    Replies: 4
    Last Post: 06-22-2010, 03:59 AM
  4. [SOLVED] Key binding Q
    By playwin2 in forum New To Java
    Replies: 3
    Last Post: 11-04-2008, 06:04 AM
  5. Dynamic Binding
    By javarishi in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •