Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

password masking & unmasking!!  RSS feed

 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm using a JPasswordField to get the password form the user.Now I wish to mask & unmask the password typed according to the user's wish.I wish to do this using a CheckBox as in the case of other general applications(winzip, etc.)
Plz. reply this positively.
Netharam
Happy middling with java.
 
Neil Marsden
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah - you could try this and throw in a checkbox instead of my button:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyPassword extends JFrame implements ActionListener
{
private JPasswordField passwordBox = new JPasswordField("",10);
private JTextField revealedBox = new JTextField("",10);
private JButton b1 = new JButton("Reveal");
public MyPassword(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
c.add(passwordBox);
c.add(revealedBox);
c.add(b1);
passwordBox.requestFocus();
passwordBox.setEchoChar('*');
b1.addActionListener(this);
}//constructor

public void actionPerformed(ActionEvent e)
{
String p = String.valueOf(passwordBox.getPassword());
revealedBox.setText(p);
}//actionPerformed
public static void main(String args[])
{
MyPassword mp = new MyPassword("Password Tester");
mp.setSize(400,150);
mp.setVisible(true);
}//main()
}//class
 
saager mhatre
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, putting the two JTextFields in a CardLayout would make the revelation effect even more jazzy!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!