import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
}
});
+ setKeyBindings(frame);
+
desktop.add(frame);
windowMenu.add(menuItem);
}
}
+ /**
+ * 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
+ {
+ 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
+ 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);
+ }
+
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents)
{