0

I want to create a JPasswordField where I can see the characters while typing, I tried passtxt.setEchoChar((char) 0); but it completely change the '*' to text I don't want this, I want to view text while typing character by character ( as we see in mobile browsers) thanks

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField  ");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p=new JPanel();
    p.add(new JLabel("Username"));
    p.add(new JTextField(20));
     p.add(new JLabel("Password"));
    JPasswordField jpassword = new JPasswordField(20);

    p.add(jpassword);

    Checkbox c=new Checkbox("type password and click");
    c.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                jpassword.setEchoChar('*');
            } else {
                jpassword.setEchoChar((char) 0);
            }
        }
    });
    jpassword.setEchoChar((char)0); 
    p.add(c);
    f.add(p);
    f.setSize(300, 200);
    f.setVisible(true);


  }
}
vinay negi
  • 38
  • 1
  • 7
  • 4
    Please post a [Runnable Example](http://stackoverflow.com/help/mcve) which we can copy-paste and see your program running with the same issue as you see, it should be as short as possible but with enough code so we can test. This will result in less confusion, more, faster and better answers :) – Frakcool Jan 15 '16 at 16:01
  • 1
    The JPasswordField doesn't have such a functionality (because it's not really required in a PC environment), but you can extend the class and implement it yourself pretty quickly... – Codebender Jan 15 '16 at 16:03
  • how can i extend the class to implement it – vinay negi Jan 15 '16 at 16:33
  • @vinaynegi If you don't know how to extend a class, please get a beginner Java book. This is really not the place to learn Java 101 like inheritance. – m0skit0 Jan 15 '16 at 16:47
  • You want to display the character for a short duration before hiding it? – user1803551 Jan 15 '16 at 17:00
  • Then I'm sure this was already asked, I remember a very very similar question. You need to search well. I'll try too. – user1803551 Jan 15 '16 at 17:23
  • Possible duplicate of [Echo jpassword character once and then hide it](http://stackoverflow.com/questions/23780963/echo-jpassword-character-once-and-then-hide-it). – user1803551 Jan 15 '16 at 17:41

1 Answers1

-1

This is an easy solution. Use a JTextField instead of JPasswordField and a String member to store the real password seperately:

private JTextField textField = new JTextField();
private String password = "";

implement a java.awt.event.KeyListener, and overwrite keyReleased like this:

public void keyReleased(KeyEvent e) {
    if (Character.isLetter(e.getKeyChar()) || 
        Character.isDigit(e.getKeyChar())) { //Add other valid characters as needed

        //Get text and last typed character
        String text = textField.getText();
        char typed = text.charAt(text.length()-1);
        //store typed letter separately for later use
        password += (text.length() > 0) ? typed : "";

        StringBuilder sb = new StringBuilder();
        for (int c=0; c<text.length()-1; c++) {
            sb.append("*");
        }
        sb.append(typed);

        //set password with asterisks, for authentication use member: password
        textField.setText(sb.toString());
}

Add the Listener to the text field and you're done.

This will overwrite all but the last character typed in with a * any time a new letter or digit is typed.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
  • 2
    Not your down-voter, but you would not want to use a KeyListener with a JTextField as in doing so, you risk significantly adversely affecting its function. I'm not an expert in this, and have to defer to MadProgrammer's answers and comments to similar issues (one of the premier Swing experts on this site). Alternatives include use of a DocumentFilter, perhaps. – Hovercraft Full Of Eels Jan 15 '16 at 17:01
  • @HovercraftFullOfEels I don't quite agree. And if you look a this official tutorial, I'm not the only one: https://docs.oracle.com/javase/tutorial/uiswing/examples/events/KeyEventDemoProject/src/events/KeyEventDemo.java But I agree that there should be some filtering on key events. I am going to add that in a moment. – Würgspaß Jan 15 '16 at 17:14
  • @HovercraftFullOfEels I just found the question I was looking for and posted a link in the question's comments section. While it doesn't have an accepted answer, trashgod linked a very similar solution. I can't vote for duplicates anymore on this question (retracted my "no MCVE" vote), but you might want to consider looking at it. – user1803551 Jan 15 '16 at 17:48