-2

Possible Duplicate:
Limiting and formatting a JTextfield

I have a JTextfield where a user enters a "social security number". The SSN has 9 digits and 2 dashes for a total of 11 characters.

How do I limit the JTextField to 11 characters and also have 2 dashes in the textfield at the 4th and 7th position of the 11 characters?

one thought, i have is using an IF statement stating that the soc(which would be the input to the Jtextfield) has to be greater than or equal 000000001 and less than or equal to 999999999 then once that is determined. could i use a substring to for positions 0-2 assign that to varible than concatenate that with a "-" then create another subsstring for positions 3-4 and concatenate that with the prior varibles and add the last "-" and then finish the input.

is that possible? does that make logical sense?

Community
  • 1
  • 1
nico11
  • 21
  • 2
  • 2
  • 3
    [JFormattedTextField](http://download.oracle.com/javase/6/docs/api/javax/swing/JFormattedTextField.html) – PeterMmm Nov 21 '11 at 18:11
  • 5
    The answer provided to your [last question](http://stackoverflow.com/questions/8199866/limiting-and-formatting-a-jtextfield) is perfectly acceptable (and correct). What's the problem? Voting to close this question as an exact duplicate. – mre Nov 21 '11 at 18:36

1 Answers1

0

JFormattedTextField may seem helpful, but trust me, it's not. It's overly complicated and arcane to try and properly configure.

Instead, look into extending Document. By overriding insertString, replace, and delete, you can control what characters are entered where.

BenCole
  • 2,092
  • 3
  • 17
  • 26
  • 2
    "*JFormattedTextField may seem helpful, but trust me, it's not. It's overly complicated and arcane to try and properly configure.*"..You can't be serious.. – mre Nov 21 '11 at 18:19
  • Totally. JFormattedTextField, with a mask formatter in particular, is (in my experience) difficult to configure in an acceptable way. Maybe it's just me, but `Document`s and `DocumentFilter`s are a much easier route to the same objective. – BenCole Nov 21 '11 at 19:40
  • A more specific problem I've had is being able to delete all text from a JFormattedTextField. I've never been able to get it to behave properly in that case. – BenCole Nov 21 '11 at 19:41
  • 1
    @Ben: Your position is not _completely_ unfounded, but a [citation](http://stackoverflow.com/questions/1320117/why-is-jformattedtextfield-evil) or two might be informative. – trashgod Nov 21 '11 at 22:20
  • 1
    @trashgod Thanks for the help! I'll be sure to include sources from now on. :) – BenCole Nov 21 '11 at 22:30
  • -1 simply wrong in that generality: concededly not the easiest kid on the block, but definitely manageable. At the end of the day, some parsing has to be done _somewhere_ ... re-inventing the wheel is not a viable option, IMO – kleopatra Nov 22 '11 at 08:32
  • @trashgod thanks for the link (downvoted that as well ) – kleopatra Nov 22 '11 at 08:36