Input Verification

Other Swing Changes

New class InputVerifier

The purpose of this class is to help clients support smooth focus navigation through GUIs with text fields. Such GUIs often need to ensure that the text entered by the user is valid (for example, that it's in the proper format) before allowing the user to navigate out of the text field. To do this, clients create a subclass of InputVerifier and, using JComponent's setInputVerifier method, attach an instance of their subclass to the JComponent whose input they want to validate. Before focus is transfered to another Swing component that requests it, the input verifier's shouldYieldFocus method is called. Focus is transfered only if that method returns true.

Sometimes a component, such as a Cancel button or a scroll bar, should receive the focus even if input is invalid. To enable this, invoke setVerifyInputWhenFocusTarget(false) on the component.

The API change consists of a new abstract class, InputVerifier, and four new methods added to JComponent.


New methods in JComponent:

Copyright © 1999 Sun Microsystems, Inc. All Rights Reserved.

Please send comments to: This is not a subscription list.
Java Software