File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Difference between JFormatterTextField and JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Difference between JFormatterTextField and JTextField" Watch "Difference between JFormatterTextField and JTextField" New topic

Difference between JFormatterTextField and JTextField

Avani Joshi

Joined: Jul 09, 2007
Posts: 13
Can anyone please tell me the differnce between calling a setDocument method using JFormatterTextField and calling setDocument method using JTextField.

- Thanks in advance
Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

A JFormattedTextField has, as the name implies, a format for the text to be filled in. A JTextField can have any text filled in.

Usually you use JTextField. Only use JFormattedTextField if you need the input to have a specific format, e.g. phone numbers, (___) ____

How To Ask Questions How To Answer Questions
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 865
JFormattedTextField differs from JTextField in that it can support
  • locale-specific display
  • restrictions on its value
  • editing non-Strings [via setValue(Object)/getValue()]
  • incrementing/decrementing from the keyboard

  • But you ask about specifically about setDocument(), which behaves
    about the same.

    I'd say that it's rare to call setDocument() on a JFormattedTextField.
    Usually one would call setValue() instead, and let the field (and its
    formatter) manage the Document for you.
    [ October 03, 2008: Message edited by: Brian Cole ]

    bitguru blog
    It is sorta covered in the JavaRanch Style Guide.
    subject: Difference between JFormatterTextField and JTextField