Values not clearing in JFormattedTextField
Asked Answered
G

4

5

I am finding it hard to clear the value of JFormattedTextFields. How to do it?

I tried

txtJFormattedTextField.setText("");

But when focus is lost again the value that I cleared away will appear. I read the API regarding this issue.

My factory for creating JTFormattedTextFields for Date fields is below -

public static JFormattedTextField createDateField() {
        MaskFormatter maskFormatter = null;
        try {
            maskFormatter = new MaskFormatter("##/##/####");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        JFormattedTextField formattedTextField = new JFormattedTextField(
                maskFormatter);
        SwingHelper.installDateValidation((AbstractDocument) formattedTextField
                .getDocument());
        formattedTextField.setColumns(10);
        return formattedTextField;
    }

Tried the

try {
    ((JFormattedTextField) txtSigninDate).commitEdit();
} catch (ParseException e) {
    e.printStackTrace();
}

The result was an exception - below is the exception trace.

java.text.ParseException: stringToValue passed invalid value
    at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:456)
    at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:371)
    at javax.swing.JFormattedTextField.commitEdit(JFormattedTextField.java:530)
    at app.view.action.Authentication_Action.clearSigninFields(Authentication_Action.java:207)
    at app.view.action.authentication.AuthenticationCommand.decorateSignout(AuthenticationCommand.java:285)
    at app.view.action.authentication.AuthenticationCommand.executeSignin(AuthenticationCommand.java:122)
    at app.view.action.Authentication_Action$2.actionPerformed(Authentication_Action.java:292)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
Grosvenor answered 6/8, 2012 at 9:21 Comment(0)
S
6

1.depends of used Formatter

JFormattedTextField.setValue(0.00);

2.or MaskFormatter

JFormattedTextField.setValue(null);

3.is possible to set null value for JFormattedTextField with Formatter, but I'd suggest to use proper value for concrete Formatter, e.g. for NumberFormatter to set setValue(0.00)

4.for better help sooner post an SSCCE, otherwise your question probably isn't answerable,

Subtemperate answered 6/8, 2012 at 9:45 Comment(1)
I posted the Factory. Sorry for not adding a SSCCE. But I found that the JFormattedTextField.setValue(null); does the trick. ThanksGrosvenor
G
1

The reason for the behavior you are seeing is that a JFormattedTextField reverts to the last valid value on focusLost and on commit. Typically your code should interact with such a field using the getter and setter for the value, and not for text as with a regular JTextComponent.

So the solution is setting a value which is formatted by the formatter by an empty String.

Consult the tutorial for more information.

Gabie answered 6/8, 2012 at 10:5 Comment(0)
R
1

The reason the JFormattedTextField will return the old value put in when using a formatter factory is it verifies the input or lack thereof when the focus is lost from the field.

The answer to this is simple found it on the java docs website In your case this is how it works

txtJFormattedTextField.setFocusLostBehavior(JFormattedTextField.PERSIST);

add this line after the creation of your textfield and when the focus is lost it will not attempt to verify the input any longer in other words keeping the current value of the field even if it is "invalid"

https://docs.oracle.com/javase/7/docs/api/javax/swing/JFormattedTextField.html

Recommendation answered 30/11, 2014 at 22:16 Comment(1)
Welcome to Stack Overflow. Would you add a link to the java docs you mentioned?Venation
I
-1

Try adding

formattedTextField.commitEdit();

after setting text;

Injure answered 6/8, 2012 at 9:23 Comment(1)
The poster ir referring to the focus behavior, not setting values programmatically.Awful

© 2022 - 2024 — McMap. All rights reserved.