Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: JFormattedTextField

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic