एक JPasswordField, JTextField का एक सबक्लास है और JPasswordField में दर्ज प्रत्येक कैरेक्टर को एक इको कैरेक्टर द्वारा बदला जा सकता है। यह पासवर्ड के लिए गोपनीय इनपुट की अनुमति देता है। डिफ़ॉल्ट रूप से, इको कैरेक्टर एस्टेरिस्क(*) होता है। JPasswordField के महत्वपूर्ण मेथड्स हैं: getPassword(), getText(), getAccessibleContext() आदि। डिफ़ॉल्ट रूप से, JPasswordField इको कैरेक्टर्स दिखा सकता है। हम JCheckBox पर क्लिक करके इको कैरेक्टर्स छुपा सकते हैं और यूजर को मूल टेक्स्ट दिखा सकते हैं।
उदाहरण
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;
}
आउटपुट
इको कैरेक्टर दिखाएं

इको कैरेक्टर छुपाएं
