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

    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 is offline Member
    Join Date
    Jan 2011
    Rep Power


    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
    Blog Entries
    Rep Power

Similar Threads

  1. JSP and javascript binding???
    By paulb in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-02-2011, 09: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, 07: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