1 /* 2 * @(#)InputVerifier.java 1.9 03/12/19 3 * 4 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 5 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 6 */ 7 8 package javax.swing; 9 10 import java.util.*; 11 12 /** 13 * The purpose of this class is to help clients support smooth focus 14 * navigation through GUIs with text fields. Such GUIs often need 15 * to ensure that the text entered by the user is valid (for example, 16 * that it's in 17 * the proper format) before allowing the user to navigate out of 18 * the text field. To do this, clients create a subclass of 19 * <code>InputVerifier</code> and, using <code>JComponent</code>'s 20 * <code>setInputVerifier</code> method, 21 * attach an instance of their subclass to the <code>JComponent</code> whose input they 22 * want to validate. Before focus is transfered to another Swing component 23 * that requests it, the input verifier's <code>shouldYieldFocus</code> method is 24 * called. Focus is transfered only if that method returns <code>true</code>. 25 * <p> 26 * The following example has two text fields, with the first one expecting 27 * the string "pass" to be entered by the user. If that string is entered in 28 * the first text field, then the user can advance to the second text field 29 * either by clicking in it or by pressing TAB. However, if another string 30 * is entered in the first text field, then the user will be unable to 31 * transfer focus to the second text field. 32 * <p> 33 * <pre> 34 * import java.awt.*; 35 * import java.util.*; 36 * import java.awt.event.*; 37 * import javax.swing.*; 38 * 39 * // This program demonstrates the use of the Swing InputVerifier class. 40 * // It creates two text fields; the first of the text fields expects the 41 * // string "pass" as input, and will allow focus to advance out of it 42 * // only after that string is typed in by the user. 43 * 44 * public class VerifierTest extends JFrame { 45 * public VerifierTest() { 46 * JTextField tf1 = new JTextField ("Type \"pass\" here"); 47 * getContentPane().add (tf1, BorderLayout.NORTH); 48 * tf1.setInputVerifier(new PassVerifier()); 49 * 50 * JTextField tf2 = new JTextField ("TextField2"); 51 * getContentPane().add (tf2, BorderLayout.SOUTH); 52 * 53 * WindowListener l = new WindowAdapter() { 54 * public void windowClosing(WindowEvent e) { 55 * System.exit(0); 56 * } 57 * }; 58 * addWindowListener(l); 59 * } 60 * 61 * class PassVerifier extends InputVerifier { 62 * public boolean verify(JComponent input) { 63 * JTextField tf = (JTextField) input; 64 * return "pass".equals(tf.getText()); 65 * } 66 * } 67 * 68 * public static void main(String[] args) { 69 * Frame f = new VerifierTest(); 70 * f.pack(); 71 * f.setVisible(true); 72 * } 73 * } 74 * </pre> 75 * 76 * @since 1.3 77 */ 78 79 80 public abstract class InputVerifier { 81 82 /** 83 * Checks whether the JComponent's input is valid. This method should 84 * have no side effects. It returns a boolean indicating the status 85 * of the argument's input. 86 * 87 * @param input the JComponent to verify 88 * @return <code>true</code> when valid, <code>false</code> when invalid 89 * @see JComponent#setInputVerifier 90 * @see JComponent#getInputVerifier 91 * 92 */ 93 94 public abstract boolean verify(JComponent input); 95 96 97 /** 98 * Calls <code>verify(input)</code> to ensure that the input is valid. 99 * This method can have side effects. In particular, this method 100 * is called when the user attempts to advance focus out of the 101 * argument component into another Swing component in this window. 102 * If this method returns <code>true</code>, then the focus is transfered 103 * normally; if it returns <code>false</code>, then the focus remains in 104 * the argument component. 105 * 106 * @param input the JComponent to verify 107 * @return <code>true</code> when valid, <code>false</code> when invalid 108 * @see JComponent#setInputVerifier 109 * @see JComponent#getInputVerifier 110 * 111 */ 112 113 public boolean shouldYieldFocus(JComponent input) { 114 return verify(input); 115 } 116 117 } 118