Java JPasswordField get password as char array

Description

Java JPasswordField get password as char array

import java.awt.FlowLayout;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

class Demo extends JPanel {
  public Demo() {    
    setLayout(new FlowLayout());
    /*  w w w . jav a2  s  . com*/
    JPasswordField passwordField = new JPasswordField(10);
    passwordField.setActionCommand("password");
    
    passwordField.addActionListener(e->{
      String cmd = e.getActionCommand();
      System.out.println(cmd);
      
      char[] input = passwordField.getPassword();
      System.out.println(Arrays.toString(input));
      
    });
    add(passwordField);
  }
}

public class Main {
  public static void main(String[] args) {
    Demo panel = new Demo();
    JFrame application = new JFrame();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add(panel);
    application.setSize(250, 250);
    application.setVisible(true);
  }
}



PreviousNext

Related