Question

I am working with jPasswordField in java in order to make a log in system.

I have used jTextField for name input and jPasswordField for password. I want to create an option of "Show Characters" in my program that may allow the user to view the characters he has typed in passwordfield.

I have tried

  1. retrieving data from password field
  2. converting it into string
  3. displaying that string in passwordfield using .setText() method

But this did not work, as it displays everything in esteric (*****) form. Please help...

Was it helpful?

Solution

hidePasswordCheckbox.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            httpProxyPassword.setEchoChar('*');
        } else {
             httpProxyPassword.setEchoChar((char) 0);
        }
    }
});

You have to use httpProxyPassword.setEchoChar((char) 0); for that. Here httpProxyPassword is a name of textbox you can use as per your's.

OTHER TIPS

I checked it and found that default value is * but it changes according to look and feel. So check that default value and put it back in setEchoChar method also remember typecasting(char). Hope it helps

/*
 * here set defaultt an int variable and 
 * then print that value using System.out.println(defaultt);
 * and then check that value and use it in place of that default value eg:-
 */

int defaultt = psswrd.getEchoChar(); 
System.out.println(defaultt);
//now check the value
chbx_psswrd.addItemListener(new ItemListener() { 
    public void itemStateChanged(ItemEvent e) { 
        if (e.getStateChange() == ItemEvent.SELECTED) {
            psswrd.setEchoChar((char)<***keep that value here directly***>); 
        } else {  
            psswrd.setEchoChar((char) 0);  
        } 
    }
});

with this improved code you change the echo char back to its default, instead of *

char default = psswrd.getEchoChar(); 
chbx_psswrd.addItemListener(new ItemListener() { 
        public void itemStateChanged(ItemEvent e) { 
            if (e.getStateChange() == ItemEvent.SELECTED) {
                psswrd.setEchoChar(default); 
            } else {  
                psswrd.setEchoChar((char) 0);  
            } 
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top