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

Print this page




  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
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.im.InputContext;


  30 import java.io.*;
  31 import java.text.*;
  32 import java.util.*;
  33 import javax.swing.event.*;
  34 import javax.swing.plaf.UIResource;
  35 import javax.swing.text.*;
  36 
  37 /**
  38  * <code>JFormattedTextField</code> extends <code>JTextField</code> adding
  39  * support for formatting arbitrary values, as well as retrieving a particular
  40  * object once the user has edited the text. The following illustrates
  41  * configuring a <code>JFormattedTextField</code> to edit dates:
  42  * <pre>
  43  *   JFormattedTextField ftf = new JFormattedTextField();
  44  *   ftf.setValue(new Date());
  45  * </pre>
  46  * <p>
  47  * Once a <code>JFormattedTextField</code> has been created, you can
  48  * listen for editing changes by way of adding
  49  * a <code>PropertyChangeListener</code> and listening for


 159  * <code>JFormattedTextField</code> you should not install your own. If you do,
 160  * you are likely to see odd behavior in that the editing policy of the
 161  * <code>AbstractFormatter</code> will not be enforced.
 162  * <p>
 163  * <strong>Warning:</strong> Swing is not thread safe. For more
 164  * information see <a
 165  * href="package-summary.html#threading">Swing's Threading
 166  * Policy</a>.
 167  * <p>
 168  * <strong>Warning:</strong>
 169  * Serialized objects of this class will not be compatible with
 170  * future Swing releases. The current serialization support is
 171  * appropriate for short term storage or RMI between applications running
 172  * the same version of Swing.  As of 1.4, support for long term storage
 173  * of all JavaBeans&trade;
 174  * has been added to the <code>java.beans</code> package.
 175  * Please see {@link java.beans.XMLEncoder}.
 176  *
 177  * @since 1.4
 178  */

 179 @SuppressWarnings("serial") // Same-version serialization only
 180 public class JFormattedTextField extends JTextField {
 181     private static final String uiClassID = "FormattedTextFieldUI";
 182     private static final Action[] defaultActions =
 183             { new CommitAction(), new CancelAction() };
 184 
 185     /**
 186      * Constant identifying that when focus is lost,
 187      * <code>commitEdit</code> should be invoked. If in committing the
 188      * new value a <code>ParseException</code> is thrown, the invalid
 189      * value will remain.
 190      *
 191      * @see #setFocusLostBehavior
 192      */
 193     public static final int COMMIT = 0;
 194 
 195     /**
 196      * Constant identifying that when focus is lost,
 197      * <code>commitEdit</code> should be invoked. If in committing the new
 198      * value a <code>ParseException</code> is thrown, the value will be


 340     }
 341 
 342     /**
 343      * Sets the behavior when focus is lost. This will be one of
 344      * <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
 345      * <code>JFormattedTextField.REVERT</code>,
 346      * <code>JFormattedTextField.COMMIT</code> or
 347      * <code>JFormattedTextField.PERSIST</code>
 348      * Note that some <code>AbstractFormatter</code>s may push changes as
 349      * they occur, so that the value of this will have no effect.
 350      * <p>
 351      * This will throw an <code>IllegalArgumentException</code> if the object
 352      * passed in is not one of the afore mentioned values.
 353      * <p>
 354      * The default value of this property is
 355      * <code>JFormattedTextField.COMMIT_OR_REVERT</code>.
 356      *
 357      * @param behavior Identifies behavior when focus is lost
 358      * @throws IllegalArgumentException if behavior is not one of the known
 359      *         values
 360      * @beaninfo
 361      *  enum: COMMIT         JFormattedTextField.COMMIT
 362      *        COMMIT_OR_REVERT JFormattedTextField.COMMIT_OR_REVERT
 363      *        REVERT         JFormattedTextField.REVERT
 364      *        PERSIST        JFormattedTextField.PERSIST
 365      *  description: Behavior when component loses focus
 366      */






 367     public void setFocusLostBehavior(int behavior) {
 368         if (behavior != COMMIT && behavior != COMMIT_OR_REVERT &&
 369             behavior != PERSIST && behavior != REVERT) {
 370             throw new IllegalArgumentException("setFocusLostBehavior must be one of: JFormattedTextField.COMMIT, JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.PERSIST or JFormattedTextField.REVERT");
 371         }
 372         focusLostBehavior = behavior;
 373     }
 374 
 375     /**
 376      * Returns the behavior when focus is lost. This will be one of
 377      * <code>COMMIT_OR_REVERT</code>,
 378      * <code>COMMIT</code>,
 379      * <code>REVERT</code> or
 380      * <code>PERSIST</code>
 381      * Note that some <code>AbstractFormatter</code>s may push changes as
 382      * they occur, so that the value of this will have no effect.
 383      *
 384      * @return returns behavior when focus is lost
 385      */
 386     public int getFocusLostBehavior() {


 390     /**
 391      * Sets the <code>AbstractFormatterFactory</code>.
 392      * <code>AbstractFormatterFactory</code> is
 393      * able to return an instance of <code>AbstractFormatter</code> that is
 394      * used to format a value for display, as well an enforcing an editing
 395      * policy.
 396      * <p>
 397      * If you have not explicitly set an <code>AbstractFormatterFactory</code>
 398      * by way of this method (or a constructor) an
 399      * <code>AbstractFormatterFactory</code> and consequently an
 400      * <code>AbstractFormatter</code> will be used based on the
 401      * <code>Class</code> of the value. <code>NumberFormatter</code> will
 402      * be used for <code>Number</code>s, <code>DateFormatter</code> will
 403      * be used for <code>Dates</code>, otherwise <code>DefaultFormatter</code>
 404      * will be used.
 405      * <p>
 406      * This is a JavaBeans bound property.
 407      *
 408      * @param tf <code>AbstractFormatterFactory</code> used to lookup
 409      *          instances of <code>AbstractFormatter</code>
 410      * @beaninfo
 411      *       bound: true
 412      *   attribute: visualUpdate true
 413      * description: AbstractFormatterFactory, responsible for returning an
 414      *              AbstractFormatter that can format the current value.
 415      */


 416     public void setFormatterFactory(AbstractFormatterFactory tf) {
 417         AbstractFormatterFactory oldFactory = factory;
 418 
 419         factory = tf;
 420         firePropertyChange("formatterFactory", oldFactory, tf);
 421         setValue(getValue(), true, false);
 422     }
 423 
 424     /**
 425      * Returns the current <code>AbstractFormatterFactory</code>.
 426      *
 427      * @see #setFormatterFactory
 428      * @return <code>AbstractFormatterFactory</code> used to determine
 429      *         <code>AbstractFormatter</code>s
 430      */
 431     public AbstractFormatterFactory getFormatterFactory() {
 432         return factory;
 433     }
 434 
 435     /**
 436      * Sets the current <code>AbstractFormatter</code>.
 437      * <p>
 438      * You should not normally invoke this, instead set the
 439      * <code>AbstractFormatterFactory</code> or set the value.
 440      * <code>JFormattedTextField</code> will
 441      * invoke this as the state of the <code>JFormattedTextField</code>
 442      * changes and requires the value to be reset.
 443      * <code>JFormattedTextField</code> passes in the
 444      * <code>AbstractFormatter</code> obtained from the
 445      * <code>AbstractFormatterFactory</code>.
 446      * <p>
 447      * This is a JavaBeans bound property.
 448      *
 449      * @see #setFormatterFactory
 450      * @param format AbstractFormatter to use for formatting
 451      * @beaninfo
 452      *       bound: true
 453      *   attribute: visualUpdate true
 454      * description: TextFormatter, responsible for formatting the current value
 455      */
 456     protected void setFormatter(AbstractFormatter format) {
 457         AbstractFormatter oldFormat = this.format;
 458 
 459         if (oldFormat != null) {
 460             oldFormat.uninstall();
 461         }
 462         setEditValid(true);
 463         this.format = format;
 464         if (format != null) {
 465             format.install(this);
 466         }
 467         setEdited(false);
 468         firePropertyChange("textFormatter", oldFormat, format);
 469     }
 470 
 471     /**
 472      * Returns the <code>AbstractFormatter</code> that is used to format and
 473      * parse the current value.
 474      *
 475      * @return AbstractFormatter used for formatting
 476      */


 477     public AbstractFormatter getFormatter() {
 478         return format;
 479     }
 480 
 481     /**
 482      * Sets the value that will be formatted by an
 483      * <code>AbstractFormatter</code> obtained from the current
 484      * <code>AbstractFormatterFactory</code>. If no
 485      * <code>AbstractFormatterFactory</code> has been specified, this will
 486      * attempt to create one based on the type of <code>value</code>.
 487      * <p>
 488      * The default value of this property is null.
 489      * <p>
 490      * This is a JavaBeans bound property.
 491      *
 492      * @param value Current value to display
 493      * @beaninfo
 494      *       bound: true
 495      *   attribute: visualUpdate true
 496      * description: The value to be formatted.
 497      */


 498     public void setValue(Object value) {
 499         if (value != null && getFormatterFactory() == null) {
 500             setFormatterFactory(getDefaultFormatterFactory(value));
 501         }
 502         setValue(value, true, true);
 503     }
 504 
 505     /**
 506      * Returns the last valid value. Based on the editing policy of
 507      * the <code>AbstractFormatter</code> this may not return the current
 508      * value. The currently edited value can be obtained by invoking
 509      * <code>commitEdit</code> followed by <code>getValue</code>.
 510      *
 511      * @return Last valid value
 512      */
 513     public Object getValue() {
 514         return value;
 515     }
 516 
 517     /**


 527         AbstractFormatter format = getFormatter();
 528 
 529         if (format != null) {
 530             setValue(format.stringToValue(getText()), false, true);
 531         }
 532     }
 533 
 534     /**
 535      * Sets the validity of the edit on the receiver. You should not normally
 536      * invoke this. This will be invoked by the
 537      * <code>AbstractFormatter</code> as the user edits the value.
 538      * <p>
 539      * Not all formatters will allow the component to get into an invalid
 540      * state, and thus this may never be invoked.
 541      * <p>
 542      * Based on the look and feel this may visually change the state of
 543      * the receiver.
 544      *
 545      * @param isValid boolean indicating if the currently edited value is
 546      *        valid.
 547      * @beaninfo
 548      *       bound: true
 549      *   attribute: visualUpdate true
 550      * description: True indicates the edited value is valid
 551      */


 552     private void setEditValid(boolean isValid) {
 553         if (isValid != editValid) {
 554             editValid = isValid;
 555             firePropertyChange("editValid", Boolean.valueOf(!isValid),
 556                                Boolean.valueOf(isValid));
 557         }
 558     }
 559 
 560     /**
 561      * Returns true if the current value being edited is valid. The value of
 562      * this is managed by the current <code>AbstractFormatter</code>, as such
 563      * there is no public setter for it.
 564      *
 565      * @return true if the current value being edited is valid.
 566      */

 567     public boolean isEditValid() {
 568         return editValid;
 569     }
 570 
 571     /**
 572      * Invoked when the user inputs an invalid value. This gives the
 573      * component a chance to provide feedback. The default
 574      * implementation beeps.
 575      */
 576     protected void invalidEdit() {
 577         UIManager.getLookAndFeel().provideErrorFeedback(JFormattedTextField.this);
 578     }
 579 
 580     /**
 581      * Processes any input method events, such as
 582      * <code>InputMethodEvent.INPUT_METHOD_TEXT_CHANGED</code> or
 583      * <code>InputMethodEvent.CARET_POSITION_CHANGED</code>.
 584      *
 585      * @param e the <code>InputMethodEvent</code>
 586      * @see InputMethodEvent


 656                             JFormattedTextField.this.getValue(), true, true);
 657                     }
 658                 }
 659             }
 660             else if (fb == JFormattedTextField.REVERT) {
 661                 JFormattedTextField.this.setValue(
 662                     JFormattedTextField.this.getValue(), true, true);
 663             }
 664         }
 665     }
 666 
 667     /**
 668      * Fetches the command list for the editor.  This is
 669      * the list of commands supported by the plugged-in UI
 670      * augmented by the collection of commands that the
 671      * editor itself supports.  These are useful for binding
 672      * to events, such as in a keymap.
 673      *
 674      * @return the command list
 675      */

 676     public Action[] getActions() {
 677         return TextAction.augmentList(super.getActions(), defaultActions);
 678     }
 679 
 680     /**
 681      * Gets the class ID for a UI.
 682      *
 683      * @return the string "FormattedTextFieldUI"
 684      * @see JComponent#getUIClassID
 685      */

 686     public String getUIClassID() {
 687         return uiClassID;
 688     }
 689 
 690     /**
 691      * Associates the editor with a text document.
 692      * The currently registered factory is used to build a view for
 693      * the document, which gets displayed by the editor after revalidation.
 694      * A PropertyChange event ("document") is propagated to each listener.
 695      *
 696      * @param doc  the document to display/edit
 697      * @see #getDocument
 698      * @beaninfo
 699      *  description: the text document model
 700      *        bound: true
 701      *       expert: true
 702      */


 703     public void setDocument(Document doc) {
 704         if (documentListener != null && getDocument() != null) {
 705             getDocument().removeDocumentListener(documentListener);
 706         }
 707         super.setDocument(doc);
 708         if (documentListener == null) {
 709             documentListener = new DocumentHandler();
 710         }
 711         doc.addDocumentListener(documentListener);
 712     }
 713 
 714     /*
 715      * See readObject and writeObject in JComponent for more
 716      * information about serialization in Swing.
 717      *
 718      * @param s Stream to write to
 719      */
 720     private void writeObject(ObjectOutputStream s) throws IOException {
 721         s.defaultWriteObject();
 722         if (getUIClassID().equals(uiClassID)) {




  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
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.im.InputContext;
  30 import java.beans.BeanProperty;
  31 import java.beans.JavaBean;
  32 import java.io.*;
  33 import java.text.*;
  34 import java.util.*;
  35 import javax.swing.event.*;
  36 import javax.swing.plaf.UIResource;
  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


 161  * <code>JFormattedTextField</code> you should not install your own. If you do,
 162  * you are likely to see odd behavior in that the editing policy of the
 163  * <code>AbstractFormatter</code> will not be enforced.
 164  * <p>
 165  * <strong>Warning:</strong> Swing is not thread safe. For more
 166  * information see <a
 167  * href="package-summary.html#threading">Swing's Threading
 168  * Policy</a>.
 169  * <p>
 170  * <strong>Warning:</strong>
 171  * Serialized objects of this class will not be compatible with
 172  * future Swing releases. The current serialization support is
 173  * appropriate for short term storage or RMI between applications running
 174  * the same version of Swing.  As of 1.4, support for long term storage
 175  * of all JavaBeans&trade;
 176  * has been added to the <code>java.beans</code> package.
 177  * Please see {@link java.beans.XMLEncoder}.
 178  *
 179  * @since 1.4
 180  */
 181 @JavaBean
 182 @SuppressWarnings("serial") // Same-version serialization only
 183 public class JFormattedTextField extends JTextField {
 184     private static final String uiClassID = "FormattedTextFieldUI";
 185     private static final Action[] defaultActions =
 186             { new CommitAction(), new CancelAction() };
 187 
 188     /**
 189      * Constant identifying that when focus is lost,
 190      * <code>commitEdit</code> should be invoked. If in committing the
 191      * new value a <code>ParseException</code> is thrown, the invalid
 192      * value will remain.
 193      *
 194      * @see #setFocusLostBehavior
 195      */
 196     public static final int COMMIT = 0;
 197 
 198     /**
 199      * Constant identifying that when focus is lost,
 200      * <code>commitEdit</code> should be invoked. If in committing the new
 201      * value a <code>ParseException</code> is thrown, the value will be


 343     }
 344 
 345     /**
 346      * Sets the behavior when focus is lost. This will be one of
 347      * <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
 348      * <code>JFormattedTextField.REVERT</code>,
 349      * <code>JFormattedTextField.COMMIT</code> or
 350      * <code>JFormattedTextField.PERSIST</code>
 351      * Note that some <code>AbstractFormatter</code>s may push changes as
 352      * they occur, so that the value of this will have no effect.
 353      * <p>
 354      * This will throw an <code>IllegalArgumentException</code> if the object
 355      * passed in is not one of the afore mentioned values.
 356      * <p>
 357      * The default value of this property is
 358      * <code>JFormattedTextField.COMMIT_OR_REVERT</code>.
 359      *
 360      * @param behavior Identifies behavior when focus is lost
 361      * @throws IllegalArgumentException if behavior is not one of the known
 362      *         values






 363      */
 364     @BeanProperty(bound = false, enumerationValues = {
 365             "JFormattedTextField.COMMIT",
 366             "JFormattedTextField.COMMIT_OR_REVERT",
 367             "JFormattedTextField.REVERT",
 368             "JFormattedTextField.PERSIST"}, description
 369             = "Behavior when component loses focus")
 370     public void setFocusLostBehavior(int behavior) {
 371         if (behavior != COMMIT && behavior != COMMIT_OR_REVERT &&
 372             behavior != PERSIST && behavior != REVERT) {
 373             throw new IllegalArgumentException("setFocusLostBehavior must be one of: JFormattedTextField.COMMIT, JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.PERSIST or JFormattedTextField.REVERT");
 374         }
 375         focusLostBehavior = behavior;
 376     }
 377 
 378     /**
 379      * Returns the behavior when focus is lost. This will be one of
 380      * <code>COMMIT_OR_REVERT</code>,
 381      * <code>COMMIT</code>,
 382      * <code>REVERT</code> or
 383      * <code>PERSIST</code>
 384      * Note that some <code>AbstractFormatter</code>s may push changes as
 385      * they occur, so that the value of this will have no effect.
 386      *
 387      * @return returns behavior when focus is lost
 388      */
 389     public int getFocusLostBehavior() {


 393     /**
 394      * Sets the <code>AbstractFormatterFactory</code>.
 395      * <code>AbstractFormatterFactory</code> is
 396      * able to return an instance of <code>AbstractFormatter</code> that is
 397      * used to format a value for display, as well an enforcing an editing
 398      * policy.
 399      * <p>
 400      * If you have not explicitly set an <code>AbstractFormatterFactory</code>
 401      * by way of this method (or a constructor) an
 402      * <code>AbstractFormatterFactory</code> and consequently an
 403      * <code>AbstractFormatter</code> will be used based on the
 404      * <code>Class</code> of the value. <code>NumberFormatter</code> will
 405      * be used for <code>Number</code>s, <code>DateFormatter</code> will
 406      * be used for <code>Dates</code>, otherwise <code>DefaultFormatter</code>
 407      * will be used.
 408      * <p>
 409      * This is a JavaBeans bound property.
 410      *
 411      * @param tf <code>AbstractFormatterFactory</code> used to lookup
 412      *          instances of <code>AbstractFormatter</code>





 413      */
 414     @BeanProperty(visualUpdate = true, description
 415             = "AbstractFormatterFactory, responsible for returning an AbstractFormatter that can format the current value.")
 416     public void setFormatterFactory(AbstractFormatterFactory tf) {
 417         AbstractFormatterFactory oldFactory = factory;
 418 
 419         factory = tf;
 420         firePropertyChange("formatterFactory", oldFactory, tf);
 421         setValue(getValue(), true, false);
 422     }
 423 
 424     /**
 425      * Returns the current <code>AbstractFormatterFactory</code>.
 426      *
 427      * @see #setFormatterFactory
 428      * @return <code>AbstractFormatterFactory</code> used to determine
 429      *         <code>AbstractFormatter</code>s
 430      */
 431     public AbstractFormatterFactory getFormatterFactory() {
 432         return factory;
 433     }
 434 
 435     /**
 436      * Sets the current <code>AbstractFormatter</code>.
 437      * <p>
 438      * You should not normally invoke this, instead set the
 439      * <code>AbstractFormatterFactory</code> or set the value.
 440      * <code>JFormattedTextField</code> will
 441      * invoke this as the state of the <code>JFormattedTextField</code>
 442      * changes and requires the value to be reset.
 443      * <code>JFormattedTextField</code> passes in the
 444      * <code>AbstractFormatter</code> obtained from the
 445      * <code>AbstractFormatterFactory</code>.
 446      * <p>
 447      * This is a JavaBeans bound property.
 448      *
 449      * @see #setFormatterFactory
 450      * @param format AbstractFormatter to use for formatting




 451      */
 452     protected void setFormatter(AbstractFormatter format) {
 453         AbstractFormatter oldFormat = this.format;
 454 
 455         if (oldFormat != null) {
 456             oldFormat.uninstall();
 457         }
 458         setEditValid(true);
 459         this.format = format;
 460         if (format != null) {
 461             format.install(this);
 462         }
 463         setEdited(false);
 464         firePropertyChange("textFormatter", oldFormat, format);
 465     }
 466 
 467     /**
 468      * Returns the <code>AbstractFormatter</code> that is used to format and
 469      * parse the current value.
 470      *
 471      * @return AbstractFormatter used for formatting
 472      */
 473     @BeanProperty(visualUpdate = true, description
 474             = "TextFormatter, responsible for formatting the current value")
 475     public AbstractFormatter getFormatter() {
 476         return format;
 477     }
 478 
 479     /**
 480      * Sets the value that will be formatted by an
 481      * <code>AbstractFormatter</code> obtained from the current
 482      * <code>AbstractFormatterFactory</code>. If no
 483      * <code>AbstractFormatterFactory</code> has been specified, this will
 484      * attempt to create one based on the type of <code>value</code>.
 485      * <p>
 486      * The default value of this property is null.
 487      * <p>
 488      * This is a JavaBeans bound property.
 489      *
 490      * @param value Current value to display




 491      */
 492     @BeanProperty(visualUpdate = true, description
 493             = "The value to be formatted.")
 494     public void setValue(Object value) {
 495         if (value != null && getFormatterFactory() == null) {
 496             setFormatterFactory(getDefaultFormatterFactory(value));
 497         }
 498         setValue(value, true, true);
 499     }
 500 
 501     /**
 502      * Returns the last valid value. Based on the editing policy of
 503      * the <code>AbstractFormatter</code> this may not return the current
 504      * value. The currently edited value can be obtained by invoking
 505      * <code>commitEdit</code> followed by <code>getValue</code>.
 506      *
 507      * @return Last valid value
 508      */
 509     public Object getValue() {
 510         return value;
 511     }
 512 
 513     /**


 523         AbstractFormatter format = getFormatter();
 524 
 525         if (format != null) {
 526             setValue(format.stringToValue(getText()), false, true);
 527         }
 528     }
 529 
 530     /**
 531      * Sets the validity of the edit on the receiver. You should not normally
 532      * invoke this. This will be invoked by the
 533      * <code>AbstractFormatter</code> as the user edits the value.
 534      * <p>
 535      * Not all formatters will allow the component to get into an invalid
 536      * state, and thus this may never be invoked.
 537      * <p>
 538      * Based on the look and feel this may visually change the state of
 539      * the receiver.
 540      *
 541      * @param isValid boolean indicating if the currently edited value is
 542      *        valid.




 543      */
 544     @BeanProperty(visualUpdate = true, description
 545             = "True indicates the edited value is valid")
 546     private void setEditValid(boolean isValid) {
 547         if (isValid != editValid) {
 548             editValid = isValid;
 549             firePropertyChange("editValid", Boolean.valueOf(!isValid),
 550                                Boolean.valueOf(isValid));
 551         }
 552     }
 553 
 554     /**
 555      * Returns true if the current value being edited is valid. The value of
 556      * this is managed by the current <code>AbstractFormatter</code>, as such
 557      * there is no public setter for it.
 558      *
 559      * @return true if the current value being edited is valid.
 560      */
 561     @BeanProperty(bound = false)
 562     public boolean isEditValid() {
 563         return editValid;
 564     }
 565 
 566     /**
 567      * Invoked when the user inputs an invalid value. This gives the
 568      * component a chance to provide feedback. The default
 569      * implementation beeps.
 570      */
 571     protected void invalidEdit() {
 572         UIManager.getLookAndFeel().provideErrorFeedback(JFormattedTextField.this);
 573     }
 574 
 575     /**
 576      * Processes any input method events, such as
 577      * <code>InputMethodEvent.INPUT_METHOD_TEXT_CHANGED</code> or
 578      * <code>InputMethodEvent.CARET_POSITION_CHANGED</code>.
 579      *
 580      * @param e the <code>InputMethodEvent</code>
 581      * @see InputMethodEvent


 651                             JFormattedTextField.this.getValue(), true, true);
 652                     }
 653                 }
 654             }
 655             else if (fb == JFormattedTextField.REVERT) {
 656                 JFormattedTextField.this.setValue(
 657                     JFormattedTextField.this.getValue(), true, true);
 658             }
 659         }
 660     }
 661 
 662     /**
 663      * Fetches the command list for the editor.  This is
 664      * the list of commands supported by the plugged-in UI
 665      * augmented by the collection of commands that the
 666      * editor itself supports.  These are useful for binding
 667      * to events, such as in a keymap.
 668      *
 669      * @return the command list
 670      */
 671     @BeanProperty(bound = false)
 672     public Action[] getActions() {
 673         return TextAction.augmentList(super.getActions(), defaultActions);
 674     }
 675 
 676     /**
 677      * Gets the class ID for a UI.
 678      *
 679      * @return the string "FormattedTextFieldUI"
 680      * @see JComponent#getUIClassID
 681      */
 682     @BeanProperty(bound = false)
 683     public String getUIClassID() {
 684         return uiClassID;
 685     }
 686 
 687     /**
 688      * Associates the editor with a text document.
 689      * The currently registered factory is used to build a view for
 690      * the document, which gets displayed by the editor after revalidation.
 691      * A PropertyChange event ("document") is propagated to each listener.
 692      *
 693      * @param doc  the document to display/edit
 694      * @see #getDocument




 695      */
 696     @BeanProperty(expert = true, description
 697             = "the text document model")
 698     public void setDocument(Document doc) {
 699         if (documentListener != null && getDocument() != null) {
 700             getDocument().removeDocumentListener(documentListener);
 701         }
 702         super.setDocument(doc);
 703         if (documentListener == null) {
 704             documentListener = new DocumentHandler();
 705         }
 706         doc.addDocumentListener(documentListener);
 707     }
 708 
 709     /*
 710      * See readObject and writeObject in JComponent for more
 711      * information about serialization in Swing.
 712      *
 713      * @param s Stream to write to
 714      */
 715     private void writeObject(ObjectOutputStream s) throws IOException {
 716         s.defaultWriteObject();
 717         if (getUIClassID().equals(uiClassID)) {