I noticed that i was unable to cut and copy in JPasswordField
? Now how to copy/cut the selected part of the password to clipboard? Are there any methods to do this?
Asked
Active
Viewed 1,292 times
7

JavaTechnical
- 8,846
- 8
- 61
- 97
1 Answers
8
Simple, use this method
JPasswordField jt=new JPasswordField(20);
// Put client property
jt.putClientProperty("JPasswordField.cutCopyAllowed",true);
add(jt);
By default, the password in the JPasswordField
is not allowed to be cut/copied. All you need to do is to enable them.
As per the comment on disabling paste i didn't find a property, but i have achieved using this, (i dont recommend this way)
jt.getActionMap().put("a",null);
jt.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl V"),"a");
Another way, is to do override the paste()
(i recommend this way) while declaring
JPasswordField jt=new JPasswordField(20){
public void paste(){}
};
Update: I misunderstood the comment. But the above does disabling paste. However to disable any one of the copy/cut/paste, it is better if the required method that is to be disabled is overrided with no implementation in it.
If there is a much better way, i would love to hear.

JavaTechnical
- 8,846
- 8
- 61
- 97
-
3@nachokk ["it is not merely OK to ask and answer your own question, it is _explicitly_ encouraged"](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/) – DannyMo Jul 17 '13 at 17:39
-
Interesting. Is there a way to allow pasting but disable coping content from JPasswordField? – Pshemo Jul 17 '13 at 17:41
-
disagree quite not true up to Java7 – mKorbel Jul 17 '13 at 17:54
-
1@mKorbel What is not true? – JavaTechnical Jul 17 '13 at 18:20
-
in Javas6 (already tried before my comment here) I'm able to copy password from MsWord etc..., too lazy to test in Java7 – mKorbel Jul 17 '13 at 18:24