Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/InputVerifier.java
38829 views
/*1* Copyright (c) 1999, 2013, 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* The purpose of this class is to help clients support smooth focus31* navigation through GUIs with text fields. Such GUIs often need32* to ensure that the text entered by the user is valid (for example,33* that it's in34* the proper format) before allowing the user to navigate out of35* the text field. To do this, clients create a subclass of36* <code>InputVerifier</code> and, using <code>JComponent</code>'s37* <code>setInputVerifier</code> method,38* attach an instance of their subclass to the <code>JComponent</code> whose input they39* want to validate. Before focus is transfered to another Swing component40* that requests it, the input verifier's <code>shouldYieldFocus</code> method is41* called. Focus is transfered only if that method returns <code>true</code>.42* <p>43* The following example has two text fields, with the first one expecting44* the string "pass" to be entered by the user. If that string is entered in45* the first text field, then the user can advance to the second text field46* either by clicking in it or by pressing TAB. However, if another string47* is entered in the first text field, then the user will be unable to48* transfer focus to the second text field.49*50* <pre>51* import java.awt.*;52* import java.util.*;53* import java.awt.event.*;54* import javax.swing.*;55*56* // This program demonstrates the use of the Swing InputVerifier class.57* // It creates two text fields; the first of the text fields expects the58* // string "pass" as input, and will allow focus to advance out of it59* // only after that string is typed in by the user.60*61* public class VerifierTest extends JFrame {62* public VerifierTest() {63* JTextField tf1 = new JTextField ("Type \"pass\" here");64* getContentPane().add (tf1, BorderLayout.NORTH);65* tf1.setInputVerifier(new PassVerifier());66*67* JTextField tf2 = new JTextField ("TextField2");68* getContentPane().add (tf2, BorderLayout.SOUTH);69*70* WindowListener l = new WindowAdapter() {71* public void windowClosing(WindowEvent e) {72* System.exit(0);73* }74* };75* addWindowListener(l);76* }77*78* class PassVerifier extends InputVerifier {79* public boolean verify(JComponent input) {80* JTextField tf = (JTextField) input;81* return "pass".equals(tf.getText());82* }83* }84*85* public static void main(String[] args) {86* Frame f = new VerifierTest();87* f.pack();88* f.setVisible(true);89* }90* }91* </pre>92*93* @since 1.394*/959697public abstract class InputVerifier {9899/**100* Checks whether the JComponent's input is valid. This method should101* have no side effects. It returns a boolean indicating the status102* of the argument's input.103*104* @param input the JComponent to verify105* @return <code>true</code> when valid, <code>false</code> when invalid106* @see JComponent#setInputVerifier107* @see JComponent#getInputVerifier108*109*/110111public abstract boolean verify(JComponent input);112113114/**115* Calls <code>verify(input)</code> to ensure that the input is valid.116* This method can have side effects. In particular, this method117* is called when the user attempts to advance focus out of the118* argument component into another Swing component in this window.119* If this method returns <code>true</code>, then the focus is transfered120* normally; if it returns <code>false</code>, then the focus remains in121* the argument component.122*123* @param input the JComponent to verify124* @return <code>true</code> when valid, <code>false</code> when invalid125* @see JComponent#setInputVerifier126* @see JComponent#getInputVerifier127*128*/129130public boolean shouldYieldFocus(JComponent input) {131return verify(input);132}133134}135136137