Problem:
I want to know whether can we get the JTextfield type from the DocumentFilter??
I have read java docs and searched a lot but did not find any information.
Need: I set same DocumentFilter on 2 JTextfields. When the event occur, in the "replace" method of DocumentFilter, I want to know action on which textfield resulted in the event.
Use Case:
I have near about 15 texfields(may increase in future) and I want separate checks on all which are:
- maximum text length for input
- only certain characters must be entered(this will be same for all textfields)
For e.g: textfield1 can take maximum 10 chars but textfield2 can take maximum 5 chars.
What I have done so far:
I have created separate filters for every textfield which results in lots of redundant code and increases complexity to handle, for future use.
Looking forward for any help. :-)
Ok, now your need is more clear but, once again, you can't (and you should not) check which textfield are you typing in from inside the
DocumentFilterto decide what to do in yourreplace ()method.Possible solution: create your own
DocumentFilterclass, passing all the parameters you need (maximum text length, and maybe an array of forbidden characters, or whatever else). All your textfields will have a distinctDocumentFilterinstance, with the parameters set in order to suit your needs. This way you will have a clean solution without reduntant code.For example: