Results 1 to 2 of 2
Thread: My key bindings don't work...
- 11-16-2013, 07:43 PM #1
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.
// 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 09:01 PM.Get in the habit of using standard Java naming conventions!
- 11-16-2013, 11:28 PM #2
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!
- By msphelix in forum Advanced JavaReplies: 2Last Post: 11-01-2012, 04:04 PM
- By kjkrum in forum AWT / SwingReplies: 1Last Post: 04-15-2012, 07:33 AM
- By gib65 in forum AWT / SwingReplies: 2Last Post: 10-14-2010, 04:44 PM
- By Stipejava in forum NetBeansReplies: 0Last Post: 10-30-2009, 01:15 PM
- By levent in forum Java SoftwareReplies: 0Last Post: 05-24-2007, 10:12 AM