Validation through JFormattedTextFields in Java
I want to have two textfields which will have the following validations
- Both should be Integers
- The value in one textfield should be a integer multiple of other
I have used two JFormattedTextFields to this. This is what i have done till now
NumberFormat updateFormat,sampleFormat;
updateFormat = NumberFormat.getNumberInstance();
upd开发者_开发百科ateFormat.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 the help of this my first part is complete Now I am stuck with the second part, i.e. jFormattedTextField1 value should be a Integer multiple of jFormattedTextField2. I think I need to customize my NumberFormat by extending it. But How should I do that? Please help me out
Also consider using an InputVerifier
, as discussed in Validating Input.
NumberFormat won't work for the "second" part of your problem since the limitation isn't a data format limitation but rather a data value limitation. I think that you will need to use a DocumentFilter for the second part. To learn more on how to use these, please have a look here: Implementing a Document Filter
精彩评论