How do enable cut, copy in JPasswordField?

孤人 提交于 2019-12-22 05:44:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17706393/how-do-enable-cut-copy-in-jpasswordfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!