• Post Reply Bookmark Topic Watch Topic
  • New Topic

Preventing Copy-Pasting in a JPasswordField  RSS feed

 
Bhiku Mhatre
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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?
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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
  • 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!!
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
No problem.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!