Results 1 to 2 of 2
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default My key bindings don't work...

    Why would key bindings work with the WHEN_IN_FOCUSED_WINDOW input map, but not the WHEN_FOCUSED input map? The component is focused. There is a key listener on it, and it works. (The key listener does not consume events that have any modifiers other than shift.) I even called setFocusable() on the component to make sure it can get focus. I don't think the actions are the problem; the same actions are in a popup menu shown by a mouse listener on the component, and they work when activated from there.

    The component is a JViewport, in case that matters somehow.

    Java Code:
    // this does not work
    viewport.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK), "copy");
    viewport.getActionMap().put("copy", copyAction);
    viewport.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK), "paste");
    viewport.getActionMap().put("paste", pasteAction);
    Last edited by kjkrum; 11-16-2013 at 08:01 PM.
    Get in the habit of using standard Java naming conventions!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: My key bindings don't work...

    I found the problem. I was misreading my own code. (Funny what you don't see when you think you know what's there.) The key listener was not on the viewport; it was on the scroll pane itself. More importantly, the scroll pane actually had the focus. I changed the key listener and the focus (set by a parent container) to the viewport instead of the scroll pane, and everything works as intended.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 2
    Last Post: 11-01-2012, 03:04 PM
  2. Unsure of whether to use key bindings
    By kjkrum in forum AWT / Swing
    Replies: 1
    Last Post: 04-15-2012, 07:33 AM
  3. can one disable key bindings on a JComponent?
    By gib65 in forum AWT / Swing
    Replies: 2
    Last Post: 10-14-2010, 04:44 PM
  4. JAXB Bindings - error while compiling Schema(s)
    By Stipejava in forum NetBeans
    Replies: 0
    Last Post: 10-30-2009, 12:15 PM
  5. genesis 3.0-RC2: Swing, SWT and Thinlet bindings
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 10:12 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
  •