Java Swing Tutorial - Java JPasswordField.getEchoChar()








Syntax

JPasswordField.getEchoChar() has the following syntax.

public char getEchoChar()

Example

In the following code shows how to use JPasswordField.getEchoChar() method.

import java.awt.BorderLayout;
/*ww  w . j a  va  2 s.c om*/
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box rowOne = Box.createHorizontalBox();
    rowOne.add(new JLabel("Username"));
    rowOne.add(new JTextField());
    Box rowTwo = Box.createHorizontalBox();
    rowTwo.add(new JLabel("Password"));
    JPasswordField jpassword = new JPasswordField();
    
    rowTwo.add(jpassword);
    f.add(rowOne, BorderLayout.NORTH);
    f.add(rowTwo, BorderLayout.SOUTH);
    f.setSize(300, 200);
    f.setVisible(true);
    
    jpassword.setEchoChar('#');
    System.out.println(jpassword.getEchoChar());
    
  }
}