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