The InputVerifier Class

InputVerifier is an abstract class introduced in SDK 1.3. Subclasses of InputVerifier can be attached to JComponents to control whether they are willing to give up focus. Typically, this is done to force the user to put a component in a “valid” state before allowing the user to transfer focus to another component.

A brief example of using an InputVerifier with a JFormattedTextField appeared earlier in this chapter. (See the focusLostBehavior property of the JFormattedTextField class.)

Public Methods

public abstract boolean verify(JComponent input)

Return true if the component should give up focus, or false to keep focus. You should implement this method to examine the component and make the determination, but you should not produce any side effects on the component.

public boolean shouldYieldFocus(JComponent input)

This method is called when the user attempts to transfer focus from the given component elsewhere. Return true if the component should give up focus, or false to keep focus. This method is permitted to produce side effects. InputVerifier’s implementation simply defers to the verify( ) method, but implementing classes may override either. Typically, subclasses provide only a verify( ) method unless they intend to produce side effects.

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.