Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
Every text component already has a reference to the default keymap, containing the backspace-deletes-character-to-the-left functionality etc.
We retrieve it by issuing tf.getKeymap() and have a new javax.swing.text.KeyMap created for us. This way, the default map is our new map's parent, because we want to retain the default functionality.
We then get a KeyStroke reference for the escape key ...
and associate it in the map with our Erase instance,...
a subclass of TextAction.
It contains the actionPerformed method where we clear the textfield.
getTextComponent is a special method, that allows us to use this class for other textcomponents as well.
Ctrl-Enter in the textfield adds its text to the textarea and clears the textfield.
DON'T FORGET to install the new map.
Changes to the default keymap apply to ALL text-components. That's why we use a new one.
AbstractAction is more generic, could be used to also associate with a button or menuitem.