Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField, Credit Card Number "masking"  RSS feed

 
Hector von Doodle
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!

Im looking for info on "masking" sensitive date. Specifically, I want to obscure all but the last 4 digits of a credit card number, ex
9999999999999999 is displayed as ##############9999

Hold on - here's the kicker:
1) Only the "view" is obscured - the underlying model/value is not altered.
2) Im working with a JTextField

Im thinking of something like javax.swing.table.TableCellRenderer.

Yes, I did look at JPasswordField. I can get this to work if I supply my own iml of javax.swing.text.PasswordView. I would prefer not going this route.

Please help before I have to resort to subclass'n JTextField and overriding paint()
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is the JFormattedText field. It uses the AbstractFormatter whose valueToString(Object value) can be overridden to achieve the desired result.
 
Brian Cole
Author
Ranch Hand
Posts: 935
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hector von Doodle:
I want to obscure all but the last 4 digits of a credit card number, ex
9999999999999999 is displayed as ##############9999

Hold on - here's the kicker:
1) Only the "view" is obscured - the underlying model/value is not altered.
2) Im working with a JTextField

Im thinking of something like javax.swing.table.TableCellRenderer.

Yes, I did look at JPasswordField. I can get this to work if I supply my own iml of javax.swing.text.PasswordView. I would prefer not going this route.

Please help before I have to resort to subclass'n JTextField and overriding paint()


This is actually fairly difficult to do with swing. Even though you don't prefer it, I think your best bet is to override PasswordView. (It may be easier to override FieldView, which is PasswordView's superclass.)

If you like TableCellRenderer you can use a one-cell table instead of a text field, but it doesn't buy you anything if you need the cell editor to obscure the number also.

I don't see how JFormattedTextField helps you any. It's not set up to have an actual value that can't be completely determined from the displayed value.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!