Keyboard event handling in Java swing

Source: Internet
Author: User

During Java swing programming, you often need to handle keyboard events, such as handling shortcuts. Here's how to define keyboard events, and how to handle these events.

In jdk1.2, different methods of handling keyboard events are tailored to the objects of the JComponent and text classes: In JComponent, the Registerkeyboardaction method is defined and used to bind the keyboard events that need to be handled together with the behavior that handles the event. The text class has a Keymap object, similar to the processing method in JComponent, which holds the keyboard events and corresponding behaviors that need to be handled.

In jdk1.3, a new method is used to handle keyboard events, which integrates the two jdk1.2 methods. There is no need to distinguish between components that are processed jcomponent or text types. It defines two new classes: InputMap and ActionMap. They are all simple tables or mappings. A inputmap corresponds an keystroke to an object, ActionMap an object to a behavior (action). Usually the object in the InputMap keystroke is a string that can be used to find the corresponding behavior in the ActionMap.

The Put method is used in both InputMap and ActionMap. InputMap's put method can map keystroke to an object, and ActionMap's put method can map an object to an action.

In each jcomponent component, there will be three default InputMap and one default ActionMap. They can be obtained by calling Getinputmap (int condition) and Getactionmap (). The three inputmap are InputMap (when_focused) when the component itself has focus, inputmap when the ancestor of the component has the focus (when_ancestor_of_focused_ COMPONENT) and InputMap (When_in_focused_window) with the focus of the form on which the component resides (in parentheses, the parameters that should be set in Getinputmap in order to get these inputmap). These three types of InputMap are described below:

1, the component itself has the focus of the InputMap: When the component has the focus, the keyboard keys pressed, then Java in this inputmap find the corresponding Keystroke object keyboard events.

2, the InputMap when the ancestor of the component has the focus: when the component's ancestors have the focus, the keyboard presses, Java looks for the inputmap.

3, the component's window has the focus of the InputMap: When the component is in the window with focus, the keyboard press, then Java look for this inputmap.

When a key is pressed, the event is transformed into a keystroke object, and Java looks for the corresponding inputmap of the jcomponent (for example, when the ancestor of the component has the focus, Java finds this keystroke in the inputmap that has the focus of this jcomponent ancestor, and if so, takes out its corresponding object (usually a string) and uses this object to find it in the actionmap of this jcomponent, If the corresponding behavior (action) is found, Java executes the actionperformed method of this behavior (followed by this method). To achieve the purpose of handling keyboard events.

Each inputmap can have the parent property, and the value of this property is a inputmap. When the keystroke of a keyboard event is not found in a inputmap, Java automatically looks up in the InputMap specified by its Parent property, and then looks up until it is found. The advantage of using parent is that when there are fixed keyboard mappings that do not want users to make changes, they can be stored in the InputMap specified by the Parent property to avoid accidental modification In addition, you can have the same parent for the default InputMap settings for multiple jcomponent so that you can share some of the settings for keyboard bindings. Its parent property can be set by the SetParent () method of the InputMap class. ActionMap also has the same parent property, and the same method is used.

The above is how to map a keyboard event to an action, and the following is a simple introduction to behavior (action).

Behavior is a class that implements the action interface. 7 methods are defined in the action interface. The most critical of these is the actionperformed () method. This method describes the specific procedure for this behavior. Several other methods include Setenabled,isenabled,putvalue,getvalue,addpropertychangelistener, and Removepropertychangelistener methods. They are used to set the properties of whether the behavior is available, to determine the status of the action available, to set up and to take action, and the last two methods are used to allow other objects to be notified when the properties of the action object change.

Usually we use an abstract class AbstractAction class that implements most of the action interface as the base class, overloading the Actionperformed method to implement our behavior.

We use an example to specify how to do the actual operation.

Start by writing a specific behavior that handles the specified keyboard event:

public class TextAction extends AbstractAction
{
  private String a;
  public TextAction(String a)
  { this.a = a; }
   public void actionPerformed(ActionEvent parm1)
   {
    String b = parm1.getActionCommand(); //得到行为的命令字符串
    System.out.println("command="+b);
    System.out.println("prompt="+this.a);
   }
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.