Detecting KeyBoard events on a JDialog  
Author Message
denzel





PostPosted: 2006-10-30 20:02:00 Top

java-programmer, Detecting KeyBoard events on a JDialog Hello,

i have a JDialog subclass, with a lot of components in it, and i would
like to detect Ctrl+C on it. I've tried to use this:

protected void register(JComponent jcomp) {
Action action = new AbstractAction("Action") {
public void actionPerformed(ActionEvent evt) {
System.err.println("Action!!!");
}
};

KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_C,
KeyEvent.CTRL_DOWN_MASK);

jcomp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, "myaction");
jcomp.getInputMap(JComponent.WHEN_FOCUSED).put(key, "myaction");
jcomp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(key,
"myaction");

jcomp.getActionMap().put("myaction", action);
}

wich is call like that:

register((JComponent)getContentPane());
register((JComponent)getGlassPane());
register(getRootPane());

but my action is never performed.

So i've registered a KeyListener recursively on my dialog, and the
keylistener is called with getSource() returning the dialog ref. But the
dialog has no inputMap so how can i do?

I can't use the recursive trick because there are 2 JTables in the
Dialog which must keep its default copy behavior, and a lot of text
fields too.

Thanks for any help.