< prev index next >

src/java.desktop/share/classes/javax/swing/JFormattedTextField.java

Print this page




  42  * object once the user has edited the text. The following illustrates
  43  * configuring a <code>JFormattedTextField</code> to edit dates:
  44  * <pre>
  45  *   JFormattedTextField ftf = new JFormattedTextField();
  46  *   ftf.setValue(new Date());
  47  * </pre>
  48  * <p>
  49  * Once a <code>JFormattedTextField</code> has been created, you can
  50  * listen for editing changes by way of adding
  51  * a <code>PropertyChangeListener</code> and listening for
  52  * <code>PropertyChangeEvent</code>s with the property name <code>value</code>.
  53  * <p>
  54  * <code>JFormattedTextField</code> allows
  55  * configuring what action should be taken when focus is lost. The possible
  56  * configurations are:
  57  *
  58  * <table class="striped">
  59  * <caption>Possible JFormattedTextField configurations and their descriptions
  60  * </caption>
  61  * <thead>
  62  * <tr><th>Value</th>
  63  * <th>Description</th></tr>

  64  * </thead>
  65  * <tbody>
  66  * <tr><td>JFormattedTextField.REVERT
  67  *            <td>Revert the display to match that of <code>getValue</code>,
  68  *                possibly losing the current edit.
  69  *        <tr><td>JFormattedTextField.COMMIT
  70  *            <td>Commits the current value. If the value being edited
  71  *                isn't considered a legal value by the
  72  *                <code>AbstractFormatter</code> that is, a
  73  *                <code>ParseException</code> is thrown, then the value
  74  *                will not change, and then edited value will persist.
  75  *        <tr><td>JFormattedTextField.COMMIT_OR_REVERT
  76  *            <td>Similar to <code>COMMIT</code>, but if the value isn't
  77  *                legal, behave like <code>REVERT</code>.
  78  *        <tr><td>JFormattedTextField.PERSIST
  79  *            <td>Do nothing, don't obtain a new
  80  *                <code>AbstractFormatter</code>, and don't update the value.



  81  * </tbody>
  82  * </table>

  83  * The default is <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
  84  * refer to {@link #setFocusLostBehavior} for more information on this.
  85  * <p>
  86  * <code>JFormattedTextField</code> allows the focus to leave, even if
  87  * the currently edited value is invalid. To lock the focus down while the
  88  * <code>JFormattedTextField</code> is an invalid edit state
  89  * you can attach an <code>InputVerifier</code>. The following code snippet
  90  * shows a potential implementation of such an <code>InputVerifier</code>:
  91  * <pre>
  92  * public class FormattedTextFieldVerifier extends InputVerifier {
  93  *     public boolean verify(JComponent input) {
  94  *         if (input instanceof JFormattedTextField) {
  95  *             JFormattedTextField ftf = (JFormattedTextField)input;
  96  *             AbstractFormatter formatter = ftf.getFormatter();
  97  *             if (formatter != null) {
  98  *                 String text = ftf.getText();
  99  *                 try {
 100  *                      formatter.stringToValue(text);
 101  *                      return true;
 102  *                  } catch (ParseException pe) {




  42  * object once the user has edited the text. The following illustrates
  43  * configuring a <code>JFormattedTextField</code> to edit dates:
  44  * <pre>
  45  *   JFormattedTextField ftf = new JFormattedTextField();
  46  *   ftf.setValue(new Date());
  47  * </pre>
  48  * <p>
  49  * Once a <code>JFormattedTextField</code> has been created, you can
  50  * listen for editing changes by way of adding
  51  * a <code>PropertyChangeListener</code> and listening for
  52  * <code>PropertyChangeEvent</code>s with the property name <code>value</code>.
  53  * <p>
  54  * <code>JFormattedTextField</code> allows
  55  * configuring what action should be taken when focus is lost. The possible
  56  * configurations are:
  57  *
  58  * <table class="striped">
  59  * <caption>Possible JFormattedTextField configurations and their descriptions
  60  * </caption>
  61  * <thead>
  62  *   <tr>
  63  *     <th scope="col">Value
  64  *     <th scope="col">Description
  65  * </thead>
  66  * <tbody>
  67  *   <tr>
  68  *     <th scope="row">JFormattedTextField.REVERT
  69  *     <td>Revert the display to match that of {@code getValue}, possibly losing
  70  *     the current edit.
  71  *   <tr>
  72  *     <th scope="row">JFormattedTextField.COMMIT
  73  *     <td>Commits the current value. If the value being edited isn't considered
  74  *     a legal value by the {@code AbstractFormatter} that is, a
  75  *     {@code ParseException} is thrown, then the value will not change, and
  76  *     then edited value will persist.
  77  *   <tr>
  78  *     <th scope="row">JFormattedTextField.COMMIT_OR_REVERT
  79  *     <td>Similar to {@code COMMIT}, but if the value isn't legal, behave like
  80  *     {@code REVERT}.
  81  *   <tr>
  82  *     <th scope="row">JFormattedTextField.PERSIST
  83  *     <td>Do nothing, don't obtain a new {@code AbstractFormatter}, and don't
  84  *     update the value.
  85  * </tbody>
  86  * </table>
  87  *
  88  * The default is <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
  89  * refer to {@link #setFocusLostBehavior} for more information on this.
  90  * <p>
  91  * <code>JFormattedTextField</code> allows the focus to leave, even if
  92  * the currently edited value is invalid. To lock the focus down while the
  93  * <code>JFormattedTextField</code> is an invalid edit state
  94  * you can attach an <code>InputVerifier</code>. The following code snippet
  95  * shows a potential implementation of such an <code>InputVerifier</code>:
  96  * <pre>
  97  * public class FormattedTextFieldVerifier extends InputVerifier {
  98  *     public boolean verify(JComponent input) {
  99  *         if (input instanceof JFormattedTextField) {
 100  *             JFormattedTextField ftf = (JFormattedTextField)input;
 101  *             AbstractFormatter formatter = ftf.getFormatter();
 102  *             if (formatter != null) {
 103  *                 String text = ftf.getText();
 104  *                 try {
 105  *                      formatter.stringToValue(text);
 106  *                      return true;
 107  *                  } catch (ParseException pe) {


< prev index next >