Search code examples
javaswinguser-interfacejtextfield

getting format of JFormatTextField


I've several text fields with certain formats: percent format, date format, etc:

     DateFormat dfm= new SimpleDateFormat("yyyy-MM-dd");
     textField = new JFormattedTextField(dfm);

Later I want to check what format is being used in a textField. I have checked if there is a method textField.getFormat() or similar. Didn't see anything relevant. Is there a way to know this?


Solution

  • As you can see in the source code, your Format is converted to a JFormattedTextField.AbstractFormatterFactory

    public JFormattedTextField(java.text.Format format) {
      this();
      setFormatterFactory(getDefaultFormatterFactory(format));
    }
    

    There is a getter available on JFormattedTextField to retrieve this JFormattedTextField.AbstractFormatterFactory afterwards. This JFormattedTextField.AbstractFormatterFactory gives you access to a JFormattedTextField.AbstractFormatter.

    However, you can no longer access your actual Format instance. Depending on why you need it, the JFormattedTextField.AbstractFormatter might be sufficient