In this section, you will learn about the input map. Input map is used to perform actions according to the key strokes that mean when you press any combination of keys the program performs a specific task for the specified component. Specific task for the component is assigned for the specific key combination.
This program shows how to set the key strokes for the specific component to perform a specific task. This program represents a text area on the frame in which you can write text and perform some operation on the text by applying some specified short-cut keys. These keys perform some operations like: cut, copy, paste, up, down, left and right. Following methods and APIs have been used in this program to perform some specific tasks according to the specified key combination:
This is the class of javax.swing.* package. This class is used to create an input map. It performs the work with the ActionMap. And input map is created for performing some specific task according to the specified key combination.
This is the method of the JComponent class which returns the input map when the component is focused.
This is the method of KeyStroke class which is imported from the javax.swing.*; package. This method returns the instance of the KeyStroke class which determines the specified key combination to perform the specified operations. This method takes different-different arguments. Here, the method has taken two arguments, first is the numeric key code and another is the modifier.
put(KeyStroke key, Object action_name):
This is the method of the InputMap class. This method add key combination to an action map. It takes two arguments in which, first is the object of the KeyStroke class and another is the action name which has to be performed.
This is the class of javax.swing.* package which is used for applying cut operations on the text. This class cuts the text from the text area and stores text to the system clipboard after applying the operation.
This is the class which is used to applying copy operation for the specified text of the text area. This class simply send the selected text to the system clipboard.
This is the class which is used to the paste the system clipboard text to the specified component.
Here is the code of program:
Screen shot for the result of the above program:
Liked it! Share this Tutorial