Tuesday, March 21, 2017

java: hide password input from command line and GUI component



A. If you are running the java program from a command line

java.io.Console console = System.console();

if (console != null) {
      char[] passwordChars = console.readPassword("Password: ");
      String password = new String(passwordChars);

}


B. Use JPasswordField if you are not running the program from a command line

String password = "";

final JPasswordField jpf = new JPasswordField(); 
int opt = JOptionPane.showConfirmDialog(null, jpf, "Password",                                                     JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 
if (opt == JOptionPane.OK_OPTION) {
      password = new String( jpf.getPassword() );
}

---------------------------------------------------------------------------------------------------

                        


If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment