![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
The second class introduced in the previous example is the JFC KeyStroke class. This class encapsulates a key being typed on the keyboard and is the subject of the next section. Changing the condition in the registerKeyboardAction method from WHEN_FOCUS to WHEN_IN_FOCUSED_WINDOW, shown next, will cause the textActionListener to get called when the Escape key is entered in any of the JTextField instances in the panel. If there were more components in the same window as the ageText component, the Escape key being typed in any of those components would cause the textActionListener to be invoked. ageText.registerKeyboardAction( textActionListener, KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), JComponent.WHEN_IN_FOCUSED_WINDOW ); A similar effect can be obtained by changing the WHEN_IN_FOCUSED_WINDOW condition to WHEN_ANCESTOR_OF_FOCUSED_COMPONENT and registering the keyboard action with the JPanel instance instead of the ageText JTextField instance. panel.registerKeyboardAction( textActionListener, KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); Because the three JTextField instances in this example are children of the JPanel instance, the textActionListener is invoked when the Escape key is entered in any of the instances. Unlike the previous example, if there were other panels that were siblings to this panel, the textActionListener would not be invoked if the Escape key was entered in the sibling panels. However, if the other components were also children of the panel, the textActionListener would be invoked. It is interesting to notice that the source of the event delivered to the ActionListener is the component on which the keyboard action is registered. This may or may not be the component where the event originated. For instance, in the previous example the source of the event is the JPanel instance, not the JTextField instance where the keystroke was typed. There is an overloaded version of the registerKeyboardAction method that contains a String parameter. This methods signature is shown next. When this version of the method is used, the String parameter is specified as the action command in the ActionEvent delivered to the ActionListener when the KeyStroke is received. public void registerKeyboardAction(ActionListener anAction, String aCommand, KeyStroke aKeyStroke, int aCondition ) If a keyboard action is registered on a component that exactly matches an existing keyboard action registered for that component, the new action replaces the old action. The KeyStroke must exactly match the existing KeyStroke, including modifiers. A keyboard action can be removed from a component by using the unregisterKeyboardAction method. This method requires one parameter, the KeyStroke that fires the action. If there is not an action registered for the KeyStroke instance specified in the unregisterKeyboardAction call, the method does nothing. All registered keyboard actions can be removed from an instance of a JComponent by using the resetKeyboardActions method. This method clears the Hashtable used internally to store registered KeyStrokes. The keystrokes registered on a JComponent instance can be queried with the getRegisteredKeyStrokes method. This method returns an array of KeyStroke instances. As mentioned earlier in this section, the keyboard actions are stored in a Hashtable client property in the JComponent class. Thus the Hashtable is enumerated and its elements are stored in an array before being returned to the caller. If there are no keyboard actions registered for the instance, an empty array (an array of length 0) is returned. The condition for a KeyStroke can be queried by using the getConditionForKeyStroke method. This method takes the KeyStroke to be checked as a parameter and returns the condition specified for that KeyStroke, or UNDEFINED_CONDITION if the KeyStroke has not been registered for the JComponent. The action that will be invoked when a KeyStroke is received in a JComponent instance can be queried with the getActionForKeyStroke method. This method takes a KeyStroke instance and returns the ActionListener whose actionPerformed method will be called when the KeyStroke with proper modifiers is entered. If an action is not registered for the given KeyStroke, null is returned.
|
![]() |
|