Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The KeyStroke Class

A KeyStroke instance contains its key character, key code, modifiers, and a flag for key pressed or released state. The key character is the character representation of the KeyStroke instance. The key code is the integer code defined in the KeyEvent class. The modifier field represents any modifier for the KeyStroke. Common modifiers are the Ctrl and Alt keys. The final field represents if the KeyStroke instance is a key pressed or key released keystroke.

The application does not create KeyStroke instances. Instead, the KeyStroke class caches instances, and the application obtains a handle to a KeyStroke instance with one of the overloaded getKeyStroke methods. These methods are public static methods, allowing them to be called from any other class. There are currently five variants of the getKeyStrokeMethod, shown next.

public static KeyStroke getKeyStroke(char keyChar)
public static KeyStroke getKeyStroke(char keyChar, boolean onKeyRelease)
public static KeyStroke getKeyStroke(int keyCode, int modifiers, boolean
onKeyRelease)
public static KeyStroke getKeyStroke(int keyCode, int modifiers)
public static KeyStroke getKeyStroke(String representation)

These access methods match nicely to the attributes within a KeyStroke instance. The possible modifiers for variants that take a modifier are java.awt.Event.SHIFT_MASK, java.awt.Event.CTRL_MASK, java.awt.Event.META_MASK, java.awt.Event.ALT_MASK. A KeyStroke without a modifier is specified by using 0 for the modifier. The modifiers can be logically ORed together to specify multiple modifiers. For example, to obtain the KeyStroke for the popular command Ctrl+Alt+Delete KeyStroke would be as follows:

KeyStroke.getKeyStroke( KeyEvent.VK_DELETE, CTRL_MASK | ALT_MASK ),

At the time of this writing, the getKeyStroke method taking a String parameter is not implemented.

The KeyStroke class defines a convenience method, getKeyStrokeForEvent, that will extract the KeyStroke from a KeyEvent. The following is the method’s signature:

public static KeyStroke getKeyStrokeForEvent(KeyEvent anEvent)

Once a KeyStroke instance is in hand, access methods defined in the KeyStroke class can be used to query the instance’s attributes. These methods are:

public char getKeyChar()
public int getKeyCode()
public int getModifiers()
public boolean isOnKeyRelease()

You should notice that there are no corresponding setter methods for the attributes. KeyStroke instances are immutable. This allows the instances to be cached and shared. If the instances were not immutable, one method could change the KeyStroke that is shared between many methods in an application. This of course could have disastrous consequences.

Scrolling Support

The JFC provides traditional scrolling of any component by using the JScrollPane and JViewport classes. Once the component is placed in a viewport contained in a scroll pane, scrollbars can be used to translate the view of the component. The JComponent class also supports autoscrolling. When the JComponent instance has autoscrolling enabled, the component will scroll when the mouse is dragged in the component. Thus, with autoscrolling enabled, the user of the application can scroll the component by dragging the mouse from within the component. With autoscrolling enabled, the scrollbars are not required; however, they can be used as normal.

The setAutoscrolls method is used to enable or disable autoscrolling for a JComponent instance. This method takes one boolean parameter. Passing true enables autoscrolling, and false disables autoscrolling. The getAutoscrolls method is used to query the current state of autoscrolling. Notice that the method is getAutoscrolls and not isAutoscrolling as you might have expected. Also, autoscrolling is not a bound property in the JComponent class.

The autoscrolling feature gives the JComponent developer a tremendous amount of power from within the toolkit itself. A complete autoscrolling example will be presented in Chapter 15, “Scrolling Components.”

When a JComponent is contained in a JViewport instance contained in a JScrollPane instance, the scrollRectToVisible method can be used to programmatically display any portion of the component in the viewport. This method climbs the JComponent instance’s lineage until the viewport containing the instance is found. Once the viewport is found, the scrollRectToVisible of the viewport is called. The scrollRectToVisible takes one parameter, an AWT Rectangle instance. The coordinates of the rectangle are translated as the search up the parent hierarchy is performed.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.