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