I have a problem with the class JFormattedTextField. Maybe someone has
already solved this.
Is there any possibility to restrict the valid characters depending on
the position inside the mask (besides the available distinction
Letter/Number etc).
What I want is e.g. that first char must be [0-2] second char must be
[0-4] thrid char [0-5] etc.
I think one can not do this with the standart swing classes?
Thanks for any help
Oliver Wong - 16 Sep 2005 16:43 GMT
>I have a problem with the class JFormattedTextField. Maybe someone has
> already solved this.
[quoted text clipped - 7 lines]
>
> I think one can not do this with the standart swing classes?
See the javadocs for the javax.swing.InputVerifier class.
- Oliver
Roedy Green - 16 Sep 2005 20:32 GMT
>I think one can not do this with the standart swing classes?
I did it with AWT classes, so surely you can do it with Swing. It is
just a matter of how much code you are willing to write.
see http://mindprod.com/jgloss/validation.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
bassclar - 22 Sep 2005 19:17 GMT
rspu...@yahoo.com wrote:
> I have a problem with the class JFormattedTextField. Maybe someone has
> already solved this.
[quoted text clipped - 5 lines]
> What I want is e.g. that first char must be [0-2] second char must be
> [0-4] thrid char [0-5] etc.
I'm not sure exactly what you mean by "etc." here but, depending on
how how many digits might be allowed, regular expressions might do
the trick.
My best guess is that you want to force the input string to be between
one and eight digits long. (The ninth char couldn't really be in the
[0-10] range, since 10 doesn't fit in a char.)
In this case you could do the following. It's ugly but it works.
java.util.regex.Pattern pat = java.util.regex.Pattern.compile(
"[0-2]([0-3]([0-4]([0-5]([0-6]([0-7]([0-8]([0-9])?)?)?)?)?)?)?");
JFormattedTextField ftf =
new JFormattedTextField(new RegexPatternFormatter(pat));
This uses RegexPatternFormatter from the 2nd edition of the O'Reilly
book "Java Swing":
<http://examples.oreilly.com/jswing2/code/ch20/RegexPatternFormatter.java>
It's not part of the JDK but it's only about 20 lines long and you are
allowed to use it. At least you are probably allowed to use it--see
<http://www.oreilly.com/pub/a/oreilly/ask_tim/2001/codepolicy.html>.