JFormattedTextField instead of JTextField - java

In Java, what is the difference between JTextField and JFormattedTextField ?
I mean when should I use JFormattedTextField instead of JTextField?

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
To know more about it Click here and here

Have a look at the documentation for JFormattedTextField here, essentially it adds support for formatting arbitrary date values, so it'd would be beneficial when handling date entries

JTextField is used for plain text.
JFormattedTextField is a class that extends from JTextField and it is used to set any format to the text that it contains (in example, phone numbers, e-mails, dates, etc.).
If you don't need to set any special format to the textfield, it's recommendend to use JTextField.

Related

JFormattedTextField and JComboBox

How can I use JFormattedTextField with an editable JComboBox?
I know it is using internally an JTextField, but I only want it to accept numeric values with one decimal.
For this reason I want to use JFormattedTextField, but I´m not able to find the function in JComboBox to establish it.
Maybe this will point you in the right direction:
https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification

From JTextField to JFormattedTextField

In NetBeans I've created a project with many of JTextField's. Now I understand that I should validate it. Is it possible to "cast" text fields to JFormattedTextField's?
It is possible to "cast" text fields to JFormattedTextField's?
No it's not. At least not directly. The other way around is possible though, given JFormattedTextField extends from JTextField.
So I must delete all JTexFields and add Formatted fields ?
Not necessarily but it's highly recommended. On the other hand you can let the class' variables declared as JTextField and initialize them as JFormattedTextField through custom code:
Then you can "safely" downcast them as JFormattedTextField later. However I'd like to emphasize one more time that it sounds like a dirty workaround. Also note you won't be able to change the setText(...) code to setValue(...) which is preferred for formatted text fields.

Time Formatting in a JtextArea

Is there any library that can check my jtextarea the right input time format?
for example the input format must be hh:mm:ss or hh:mm only.
You can use a JFormattedTextField with MaskFormatter, as shown in this question: JFormattedTextField with MaskFormatter
Not sure why you want to use a JTextArea for this, as it's really designed to allow the user to type free text.
Normally you would use a DocumentFilter and for more examples
Other options would include the use for a JFormattedTextField or JSpinner

custom text constrains in JTextField

How do I add custom text constrains to JTextField?
For example, I would like users to provide input in such format:
12-100
78-023
Have you tried using a JFormattedTextField? Sounds like just what you need.

Can I modify a Java Swing JTextArea so that it looks like a JPasswordField?

Can I modify a Java Swing JTextArea so that is doesn't display charaters typed? Can I make it display only '*' chars in the same way that a JPasswordField class does?
IMHO the best way to solve the problem is to look how JPasswordField overrides JTextField and to make your own derivation of JTextArea.
Well, aside from using JPasswordField (which is just a subclass of JTextField), you could replace every character with * on each key press, and store the actual text in a string or an array of chars.

Categories