0

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?

Nazerke
  • 2,098
  • 7
  • 37
  • 57

1 Answers1

2

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

Robin
  • 36,233
  • 5
  • 47
  • 99
  • If the given textfield uses date format then when submit button is pressed, text retrieved from textfield.getText(),which is String, needs to be formatted back to DateFormat: DateFormat dfm= new SimpleDateFormat("yyyy-MM-dd"); Date a = dfm.parse(textfield.getText()); – Nazerke Sep 10 '13 at 05:46
  • +1 @Nazerke: Also consider `InputVerifier`, seen [here](http://stackoverflow.com/a/2241997/230513). – trashgod Sep 10 '13 at 06:47
  • interesting that official Oracle tutorial (as always) say about – mKorbel Sep 10 '13 at 06:53