Time Formatting in a JtextArea - java

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

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.

JFormattedTextField instead of JTextField

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.

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.

Setting Time Format for jspinner in swings

I am working in a java swing application. In that application i have to take time input from user.
I need to make a JSpinner for the time, only in the hh:mm am/pm format. i searched in properties but could not get this format.
Please suggest me some way to display time in hh:mm am/pm format. I thank to all your valuable suggestions.
You can set an editor and a date model like this:
SpinnerDateModel model = new SpinnerDateModel();
model.setCalendarField(Calendar.MINUTE);
spinner= new JSpinner();
spinner.setModel(model);
spinner.setEditor(new JSpinner.DateEditor(spinner, "h:mm a"));
Thanks for answering Guillaume. Your answer is working for me after making a small change i.e.
spinner.setEditor(new JSpinner.DateEditor(spinner, "h:mm a"));
This gives me an appropriate format.

Categories