I've read several topics showing how to create a KeyBinding, however, none of them fully worked for me. My JFrame has a JMenuBar and for the items of the menu NetBeans is correctly generating code such as:
mniExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
mniExit.setText(bundle.getString("Menu.File.Exit")); // NOI18N
mniExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mniExitActionPerformed(evt);
}
});
mnuFile.add(mniExit);
However, only this binding is not visible when the menu is hidden. I've tried something like:
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(mniExit.getAccelerator(), "exit");
getRootPane().getActionMap().put("exit", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
mniExit.doClick();
}
});
But it simply does not work. What am I doing wrong?
Thanks in advance!