

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 can we show/hide the echo character of a JPasswordField in Java?
A JPasswordField is a subclass of JTextField and each character entered in a JPasswordField can be replaced by an echo character. This allows confidential input for passwords. By default, the echo character is the asterisk(*). The important methods of JPasswordField are get password(), getText(), getAccessibleContext() and etc. By default, JPasswordField can show the echo characters. We can hide the echo characters and show the original text to the use by click on JCheckBox.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; public final class ShowJPasswordTest extends JPanel { private JPasswordField pf1; private JCheckBox jcb; private JPanel panel; public ShowJPasswordTest() { pf1 = makePasswordField(); jcb = new JCheckBox("Show Passwords"); jcb.addActionListener(ae -> { JCheckBox c = (JCheckBox) ae.getSource(); pf1.setEchoChar(c.isSelected() ? '\u0000' : (Character) UIManager.get("PasswordField.echoChar")); }); panel = new JPanel(new BorderLayout()); panel.add(pf1); panel.add(jcb, BorderLayout.SOUTH); add(makeTitledPanel("Show/Hide Password", panel)); setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5)); } private static JPasswordField makePasswordField() { JPasswordField pf = new JPasswordField(20); pf.setText("tutorialspoint"); pf.setAlignmentX(Component.RIGHT_ALIGNMENT); return pf; } private static Component makeTitledPanel(String title, Component cmp) { JPanel p = new JPanel(new GridBagLayout()); p.setBorder(BorderFactory.createTitledBorder(title)); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1d; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 5, 5, 5); p.add(cmp, c); return p; } public static void main(String[] args) { JFrame frame = new JFrame("Show/HidePasswordField Test"); frame.getContentPane().add(new ShowJPasswordTest()); frame.setSize(375, 250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } } enum PasswordField { SHOW, HIDE; }
Output
Show Echo Character
Hide Echo Character
- Related Questions & Answers
- How can we show/hide the table header of a JTable in Java?
- How to set Echo Char for JPasswordField in Java?
- Can we hide the table header from a JTable in Java?
- How can we hide left/right pane of a JSplitPane programmatically in Java?
- How can we convert character array to a Reader in Java?
- How can I show and hide an HTML element using jQuery?
- How to restrict the number of digits inside JPasswordField in Java?
- In MySQL, how can we get the number code of a particular character?
- How to hide/show HTML elements in JavaScript?
- How to show and hide widgets in Tkinter?
- How can I show and hide div on mouse click using jQuery?
- How can we show a popup menu when the user right-clicks on a JComboBox in Java?
- How can we check the default character sets of a particular MySQL database?
- How to hide and show a column of a row in SAP Detail window
- How do I hide and show a menu item in the Android ActionBar?
Advertisements