Path: blob/master/src/java.desktop/share/classes/javax/swing/InputVerifier.java
41153 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import java.util.*;2829/**30* This class provides the validation mechanism for Swing components. GUIs often31* need to ensure that the components are in a valid state before allowing the32* user to navigate the input focus. To do this, clients create a subclass of33* {@code InputVerifier} and, using {@code JComponent}'s34* {@code setInputVerifier} method, attach an instance of their subclass to35* the {@code JComponent} which is the source of the focus transfer operation.36* The {@code InputVerifier} also provides the possibility to validate against37* the target of the focus transfer which may reject the focus.38* Before focus is transferred from the source Swing component to the target39* Swing component, the input verifier's40* {@code shouldYieldFocus(source, target)} method is called. Focus is41* transferred only if that method returns42* {@code true}.43* <p>44* The following example has two text fields, with the first one expecting45* the string "pass" to be entered by the user. If either that string is entered46* in the first text field or the second text field contains "accept" string,47* then the user can advance focus to the second text field by clicking in it or48* by pressing TAB.49* However, if another string is entered in the first text field and the second50* text field does not contain "accept", then the user will be unable to51* transfer focus to the second text field.52*53* <pre>54* import java.awt.*;55* import javax.swing.*;56*57* // This program demonstrates the use of the Swing InputVerifier class.58* // It creates two text fields; the first of the text fields expects the59* // string "pass" as input, and will allow focus to advance to the second text60* // field if either that string is typed in by the user or the second61* // field contains "accept" string.62*63* public class VerifierTest extends JFrame {64*65* public VerifierTest() {66* JTextField field1 = new JTextField("Type \"pass\" here");67* JTextField field2 = new JTextField("or \"accept\" here");68* getContentPane().add(field1, BorderLayout.NORTH);69* getContentPane().add(field2, BorderLayout.SOUTH);70*71* field1.setInputVerifier(new InputVerifier() {72* public boolean verify(JComponent input) {73* return "pass".equals(((JTextField) input).getText());74* }75*76* public boolean verifyTarget(JComponent input) {77* return "accept".equals(((JTextField) input).getText());78* }79*80* public boolean shouldYieldFocus(JComponent source,81* JComponent target) {82* return verify(source) || verifyTarget(target);83* }84* });85*86* pack();87* setVisible(true);88* }89*90* public static void main(String[] args) {91* SwingUtilities.invokeLater(VerifierTest::new);92* }93* }94* </pre>95*96* @since 1.397*/98public abstract class InputVerifier {99100/**101* Constructor for subclasses to call.102*/103protected InputVerifier() {}104105/**106* Checks whether the JComponent's input is valid. This method should107* have no side effects. It returns a boolean indicating the status108* of the argument's input.109*110* @param input the JComponent to verify111* @return {@code true} when valid, {@code false} when invalid112* @see JComponent#setInputVerifier113* @see JComponent#getInputVerifier114*/115public abstract boolean verify(JComponent input);116117/**118* Calls {@code verify(input)} to ensure that the input is valid.119* This method can have side effects. In particular, this method120* is called when the user attempts to advance focus out of the121* argument component into another Swing component in this window.122* If this method returns {@code true}, then the focus is transferred123* normally; if it returns {@code false}, then the focus remains in124* the argument component.125*126* @param input the JComponent to verify127* @return {@code true} when valid, {@code false} when invalid128* @see JComponent#setInputVerifier129* @see JComponent#getInputVerifier130*131* @deprecated use {@link #shouldYieldFocus(JComponent, JComponent)}132* instead.133*/134@Deprecated(since = "9")135public boolean shouldYieldFocus(JComponent input) {136return verify(input);137}138139/**140* Checks whether the target JComponent that will be receiving the focus141* is ready to accept it. This method should be over-ridden only if it is142* necessary to validate the target of the focus transfer.143* This method should have no side effects. It returns a boolean144* indicating the status of the argument's input.145*146* @implSpec By default this method returns {@code true}.147*148* @param target the target JComponent to verify149* @return {@code true} when valid, {@code false} when invalid150* @see JComponent#setInputVerifier151* @see JComponent#getInputVerifier152* @since 9153*/154public boolean verifyTarget(JComponent target) {155return true;156}157158/**159* Is called by Swing if this {@code InputVerifier} is assigned to the160* {@code source} Swing component to check whether the requested focus161* transfer from the {@code source} to {@code target} is allowed.162* This method can have side effects.163* If this method returns {@code true}, then the focus is transferred164* normally; if it returns {@code false}, then the focus remains in165* the first argument component.166*167* @implSpec The basic implementation of this method returns the conjunction168* of results obtained from {@code verify(input)} and169* {@code verifyTarget(input)} to ensure that both the source and the target170* components are in valid state.171*172* @param source the source JComponent of the focus transfer173* @param target the target JComponent of the focus transfer174* @return {@code true} when valid, {@code false} when invalid175* @see JComponent#setInputVerifier176* @see JComponent#getInputVerifier177* @since 9178*/179public boolean shouldYieldFocus(JComponent source, JComponent target) {180return shouldYieldFocus(source) && verifyTarget(target);181}182}183184185