• 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

Preventing Copy-Pasting in a JPasswordField

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I prevent pasting something into the JPasswordField? I don't want to make it uneditable, just that user HAS to type the password..he/she shouldn't be able to paste it in the password field. Any suggestions?
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
add a key listener to your Password field, which listents to keyTyped. There you can reset the text of your Password field.

This is only a workaround. Maybe there is a more clever method available. But the result is important. And at least for me it worked.
Bye
Rene
 
Bhiku Mhatre
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, this doesn't seem to work. It limits the user for typing just first letter. It does prevent someone from doing Control+V , but then it doesnt allow *TYPING* the correct password!!
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you creat your own Document perhaps a class that extends PlainDocument
then all you would have to do is override the insertString method and check the length of the String that is trying to be inserted. If its length is greater than 1 then don't do anything. If it has length then it means that the user most likely has typed it in so just call the insertString method of the super class with the String.
To create the textfield all you have to do is when you construct the textfield use the constructor with the document.
I think this is probably easier then listening for key events.
Hope this helps.
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another idea, can't you just create a custom JPasswordField class that just overrides the paste method? I haven't tried it but I think it should work.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't want to do the work for you. I simply want to point you to one right way. Of course you have to ask the KeyEvent, if CTRL+V or SHIFT+INS was pressed. Only then you have to delete the input else you do nothing. Then you can do a normal input.
 
Bhiku Mhatre
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rene. It does work and I did figure it out after posting the reply. Sorry Stupid of me.
Thanks a bunch, again.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic