• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: JFormattedTextField

 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I've created a validated text field for the ownerid by extending the PlainDocument. Ok, everything worked out fine.
Then I tried another approach using the JFormattedTextField,
JFormattedTextField ownerIDField = new JFormattedTextField(
new MaskFormatter("########");
ownerIDField.setFocusLostBehavior(JFormattedTextField.PERSIST);
Everything looked fine initially when I entered an 8-digit number. But when I pressed the <- key at the middle of the digits, it'd leave spaces in between, i.e 12 45678
Is there anyway to resolve this problem?
Thanks,
Joyce
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this is a bug. I had the same problem and looked it up.
I am leaving it as is.
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing, try catching java.lang.NumberFormatException.
This is whats returned when this situation occurs. Then you
can just flag the user.
} catch (NumberFormatException nfex) {

blah, blah, blah

}
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,
Thanks for the response. Me too, I believe it's a bug. I'll stick to the first approach, i.e. extending the PlainDocument. In this approach, there's no way a spacing would occur between the digits.
I tried to use JFormattedTextField because it required lesser code and we're advised to use it according to the Sun Tutorial. Btw, my J2SDK version is 1.4.2.1.
Have a nice weekends.
Joyce
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmmm, I got fed up with the MaskFormatter. Another problem is that I couldn't find a way to get it to accept a number of less then 8 digits - it jsut returned null. Admittedly, I don't need to be able to accept that; I think that's an option subject to interpretation. But when I found I couldn't do it with MaskFormatter, that made me think that code based around the MaskFormatter would be harder for someone else to change later. (Plus, I do want to accept user number < 8 digits; it's easier to use, and I just pad zeros on to the beginning so what's envered is a proper customer ID.) Extending PlainDocument may have taken a few more lines, but it gave me better explicit control. I think that's preferable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic