Validation through JFormattedTextFields in Java

I want to have two text fields that will have the following checks

  • Both must be intact
  • The value in one text field must be an integer multiple of another

I used two JFormattedTextFields. This is what I have done so far.

NumberFormat updateFormat,sampleFormat; updateFormat = NumberFormat.getNumberInstance(); updateFormat.setMaximumFractionDigits(0); updateFormat.setParseIntegerOnly(true); sampleFormat = NumberFormat.getNumberInstance(); sampleFormat.setMaximumFractionDigits(0); sampleFormat.setParseIntegerOnly(true); javax.swing.JFormattedTextField jFormattedTextField1 =new javax.swing.JFormattedTextField(updateFormat); javax.swing.JFormattedTextField jFormattedTextField2 = new javax.swing.JFormattedTextField(sampleFormat); 

With this help, my first part is completed. Now I am stuck with the second part, i.e. The value of jFormattedTextField1 must be an integer multiple of jFormattedTextField2. I think I need to customize my NumberFormat by expanding it. But how do I do this? Please help me

+4
source share
2 answers

Also consider using InputVerifier as described in Validating Input .

+5
source

NumberFormat will not work for the "second" part of your problem, since the limitation is not a limitation of the data format, but rather a limitation of the data value. I think you will need to use DocumentFilter for the second part. To learn more about how to use them, please see here: Implementing a Document Filter

+2
source

All Articles