- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set Echo Char for JPasswordField in Java?
With echo char, you can set a character that would appear whenever a user will type the password in the JPasswordField.
Let us first create a new JPassword field −
JPasswordField passwd = new JPasswordField();
Now, use the setEchoChar() to set the echo char for password field. Here, we have asterisk (*) as the field for password −
passwd.setEchoChar('*');
The following is an example to set echo char for password field −
Example
package my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo { public static void main(String[] args) throws Exception { JFrame frame = new JFrame("Register!"); JLabel label1, label2, label3; frame.setLayout(new GridLayout(2, 2)); label1 = new JLabel("Id", SwingConstants.CENTER); label2 = new JLabel("Age", SwingConstants.CENTER); label3 = new JLabel("Password", SwingConstants.CENTER); JTextField emailId = new JTextField(20); JTextField age = new JTextField(20); JPasswordField passwd = new JPasswordField(); passwd.setEchoChar('*'); frame.add(label1); frame.add(label2); frame.add(label3); frame.add(emailId); frame.add(age); frame.add(passwd); frame.setSize(550,250); frame.setVisible(true); } }
Output
- Related Articles
- How can we show/hide the echo character of a JPasswordField in Java?\n
- How to restrict the number of digits inside JPasswordField in Java?\n
- What is echo? Calculate the minimum distance for echo to be produced.
- How to set JAVA_HOME for Java in Windows?
- How to set JAVA_HOME for Java in Linux?
- How to set FlowLayout for JFrame in Java?
- How to set style for JTextPane in Java?
- How to assign int value to char variable in Java
- How to set tooltip text for JCheckBox in Java?
- How to set default button for JFrame in Java?
- How to set JAVA_HOME for Java in Mac OS?
- How to set border color for SoftBevelBorder in Java?
- How to set JCheckBoxMenuItem for a MenuItem in Java?
- How to set Palette Layer for JDesktopPane in Java?
- Copy char array to string in Java

Advertisements