< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  37 import javax.swing.text.*;
  38 
  39 /**
  40  * <code>JFormattedTextField</code> extends <code>JTextField</code> adding
  41  * support for formatting arbitrary values, as well as retrieving a particular
  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  * <table summary="Possible JFormattedTextField configurations and their descriptions">
  58  * <tr><th><p style="text-align:left">Value</p></th><th><p style="text-align:left">Description</p></th></tr>







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

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


   1 /*
   2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  37 import javax.swing.text.*;
  38 
  39 /**
  40  * <code>JFormattedTextField</code> extends <code>JTextField</code> adding
  41  * support for formatting arbitrary values, as well as retrieving a particular
  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 style="display:none">Possible JFormattedTextField configurations and
  60  * their descriptions</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;


< prev index next >