java - How to cut/copy text in JPasswordField as char array? -
how cut/copy password in jpasswordfield
clipboard using non-string api. thereby closing window hacker see password.
according link https://stackoverflow.com/a/8885343/2534090 char array different text.
public static void main(string[] args) { object pw = "password"; system.out.println("string: " + pw); pw = "password".tochararray(); system.out.println("array: " + pw); }
prints:
string: password array: [c@5829428e
what want in clipboard [c@5829428e
not password
i tried using stringselection
copy contents in clipboard it's constructor takes string
immutable , not safe.
you can use custom transferhandler
this.
according section swing tutorial on using , creating data flavor should able use char[] object written clipboard.
however, couldn't working , ended writing stringbuilder clipboard. commented out code attempted use char[], maybe else can figure out did wrong.
import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; import java.io.*; public class passwordhandler extends transferhandler { // public final static dataflavor char_array_flavor = new dataflavor(char[].class, "char array"); public final static dataflavor char_array_flavor = new dataflavor(stringbuilder.class, "stringbuilder"); @override public int getsourceactions(jcomponent c) { return copy; } @override public transferable createtransferable(final jcomponent c) { return new transferable() { @override public object gettransferdata(dataflavor flavor) { jpasswordfield textfield = (jpasswordfield)c; // return textfield.getpassword(); return new stringbuilder( textfield.gettext() ); } @override public dataflavor[] gettransferdataflavors() { dataflavor[] flavors = new dataflavor[1]; flavors[0] = char_array_flavor; return flavors; } @override public boolean isdataflavorsupported(dataflavor flavor) { return flavor.equals(char_array_flavor); } }; } @override public boolean canimport(transfersupport support) { boolean canimport = support.isdataflavorsupported(char_array_flavor); return canimport; } @override public boolean importdata(transfersupport support) { // char[] password; stringbuilder password; try { // password = (char[])support.gettransferable().gettransferdata(char_array_flavor); password = (stringbuilder)support.gettransferable().gettransferdata(char_array_flavor); } catch (exception e) { e.printstacktrace(); return false; } jpasswordfield textfield = (jpasswordfield)support.getcomponent(); textfield.settext(password.tostring()); return true; } private static void createandshowui() { jpasswordfield tf1 = new jpasswordfield(10); jpasswordfield tf2 = new jpasswordfield(10); transferhandler handler = new passwordhandler(); tf1.settransferhandler( handler ); tf2.settransferhandler( handler ); tf1.putclientproperty("jpasswordfield.cutcopyallowed",true); tf2.putclientproperty("jpasswordfield.cutcopyallowed",true); jframe frame = new jframe("password copy"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(tf1, borderlayout.west); frame.add(tf2, borderlayout.east); frame.add(new jtextfield(), borderlayout.south); frame.pack(); frame.setlocationbyplatform( true ); frame.setvisible( true ); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { createandshowui(); } }); } }
the code takes entire text password field. if want selected characters need modify gettransferdata()
method add selected characters stringbuilder.
Comments
Post a Comment