import java.util.concurrent.Semaphore;
import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.ActionMap;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultDesktopManager;
import javax.swing.DesktopManager;
+import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
}
/**
- * Add key bindings to a JInternalFrame so that ctrl-W and cmd-W will close
+ * Add key bindings to a JInternalFrame so that Ctrl-W and Cmd-W will close
* the window
*
* @param frame
- * the JInternalFrame to set the key bindings for
*/
private static void setKeyBindings(JInternalFrame frame)
{
- // create a close action
- class CloseAction extends AbstractAction
+ @SuppressWarnings("serial")
+ final Action closeAction = new AbstractAction()
{
- private JInternalFrame frame;
-
- CloseAction(JInternalFrame frame)
- {
- this.frame = frame;
- }
-
@Override
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
- }
+ };
- final CloseAction closeAction = new CloseAction(frame);
-
- // set up keybindings for ctrl-W and cmd-W
+ /*
+ * set up key bindings for Ctrl-W and Cmd-W, with the same (Close) action
+ */
KeyStroke ctrlWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
InputEvent.CTRL_DOWN_MASK);
KeyStroke cmdWKey = KeyStroke.getKeyStroke(KeyEvent.VK_W,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
- frame.getInputMap().put(ctrlWKey, ctrlWKey.toString());
- frame.getActionMap().put(ctrlWKey.toString(), closeAction);
- frame.getInputMap().put(cmdWKey, ctrlWKey.toString());
- frame.getActionMap().put(cmdWKey.toString(), closeAction);
+
+ InputMap inputMap = frame
+ .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
+ String ctrlW = ctrlWKey.toString();
+ inputMap.put(ctrlWKey, ctrlW);
+ inputMap.put(cmdWKey, ctrlW);
+
+ ActionMap actionMap = frame.getActionMap();
+ actionMap.put(ctrlW, closeAction);
}
@Override