1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
public abstract class MyDialog extends JDialog {
public MyDialog(Dialog arg0, boolean arg1) throws HeadlessException {
super(arg0, arg1);
initKeyManagement();
}
public MyDialog(Dialog arg0, String arg1, boolean arg2, GraphicsConfiguration arg3) throws HeadlessException {
super(arg0, arg1, arg2, arg3);
initKeyManagement();
}
public MyDialog(Dialog arg0, String arg1, boolean arg2) throws HeadlessException {
super(arg0, arg1, arg2);
initKeyManagement();
}
public MyDialog(Dialog arg0, String arg1) throws HeadlessException {
super(arg0, arg1);
initKeyManagement();
}
public MyDialog(Dialog arg0) throws HeadlessException {
super(arg0);
initKeyManagement();
}
public MyDialog(Frame arg0, boolean arg1) throws HeadlessException {
super(arg0, arg1);
initKeyManagement();
}
public MyDialog(Frame arg0, String arg1, boolean arg2, GraphicsConfiguration arg3) {
super(arg0, arg1, arg2, arg3);
initKeyManagement();
}
public MyDialog(Frame arg0, String arg1, boolean arg2) throws HeadlessException {
super(arg0, arg1, arg2);
initKeyManagement();
}
public MyDialog(Frame arg0, String arg1) throws HeadlessException {
super(arg0, arg1);
initKeyManagement();
}
public MyDialog(Frame arg0) throws HeadlessException {
super(arg0);
initKeyManagement();
}
public MyDialog(){
super();
initKeyManagement();
}
protected void initKeyManagement() {
Object escapeCommand = new Object();
// Object enterCommand = new Object();
KeyStroke ksEscape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
// KeyStroke ksEnter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
InputMap im2 = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
InputMap im3 = getRootPane().getInputMap(JComponent.WHEN_FOCUSED);
ActionMap am = getRootPane().getActionMap();
im.put(ksEscape, escapeCommand);
im2.put(ksEscape, escapeCommand);
im3.put(ksEscape, escapeCommand);
// im.put(ksEnter, enterCommand);
if (am == null)
am = new ActionMap();
am.put(escapeCommand, new MyKeyAction(ksEscape.getKeyCode()));
// am.put(enterCommand, new MyKeyAction(ksEnter.getKeyCode()));
}
protected abstract void escapeActionPerformed();
class MyKeyAction extends AbstractAction {
protected int keyCode;
public MyKeyAction(int keyCode) {
this.keyCode = keyCode;
}
public void actionPerformed(ActionEvent arg0) {
switch (keyCode) {
case KeyEvent.VK_ESCAPE :
// bCancel.doClick();
escapeActionPerformed();
break;
// case KeyEvent.VK_ENTER :
// enterActionPerformed();
// break;
default :
break;
}
}
}
} |
Partager