(don't quote sigs)
I use NetBeans and JPA annotation (hibernate). To limit a number of
caracters in a JTextField I use this code:
this.fld_cap.setDocument(new JTextFieldLimit(5));
Using a for loop I can write less code.
That's rather clever, although I can see limitations. For one thing, the
concept of "number of characters" is rather irrelevant for integers. That
normally will not substitute satisfactorily for a range check. Ergo you need
a range check anyway, ergo the limitation on widget capacity is redundant.
Elimination of the character-length loop takes even less code.
However I also can imagine rare use cases where this technique can be helpful.
I say rare because nearly all the time this type of check needs to be an
explicit validation in the model, not the view.
I am somewhat biased by working on projects where GUI screens are not the only
input channel; most systems I've worked on have EDI channels. To keep range
checks and other validations consistent irrespective of external interface,
they must be enforced in the model. Futhermore, in most systems the lengths
in the database table columns are outer bounds; for each, the actual business
rule specifies a more restricted domain of values. So in most scenarios it
doesn't really make sense for the database's physical structure to be the
arbiter of logical requirements.
But as I said, it's a neat trick and one that I now add to my arsenal.