2

In my swing application I want to echo jpassword field character for some time (1 second) and then again hide it. I want to do it character by character after user inputs a character (When user inputs a character, show it, then hide it. Then for all input characters repeat this). Can someone tell me is it possible, if yes how? Thanks in advance!

DpkTheJavaCoder
  • 117
  • 1
  • 10
  • Try the approach shown [here](http://stackoverflow.com/a/5342146/230513). – trashgod May 21 '14 at 11:07
  • @trashgod That makes the **whole** password visible, right? Not just the recently typed character. – Andreas Fester May 21 '14 at 11:10
  • Yes; now you have a [complete example](http://stackoverflow.com/help/mcve) with which to experiment; feel free to cite it if you update your question. – trashgod May 21 '14 at 11:13
  • @Andreas- I want exactly what you are saying. – DpkTheJavaCoder May 21 '14 at 11:30
  • @Andreas: Sorry, I meant to address the questioner. Thank you for helping to clarify the question. – trashgod May 21 '14 at 11:46
  • @trashgod No issue :) Its good to have a starting point, even though it might be difficult to achieve the desired behavior through `JPasswordField` - probably through some `NavigationFilter` or similar as suggested by @mKorbel in a (now deleted) comment, but probably OP needs to subclass `JTextField` and implement his own `JPasswordField` – Andreas Fester May 21 '14 at 12:02
  • 1
    I would use a regular `JTextField` and keep the real string in the background document while manually setting an `*` as the displayed text. – user1803551 May 21 '14 at 12:48
  • If you can't manage it yourself, show what you tried and I'll post some code to direct you. – user1803551 May 21 '14 at 17:17
  • @user1803551- Are u going to use DocumentListener for the real string? – DpkTheJavaCoder May 22 '14 at 10:24

2 Answers2

0

It's not very complicated, you can disable the masking characters when you set this value to “0″ with this method: setEchoChar((char) 0)

pass.getDocument().addDocumentListener(new DocumentListener() {
    public void changedUpdate(DocumentEvent e) {
        unhide();
    }
    public void removeUpdate(DocumentEvent e) {
        unhide();
    }
    public void insertUpdate(DocumentEvent e) {
        unhide();
    }

    public void unhide(){
        pass.setEchoChar((char) 0);//display password
        //here your timer
        pass.setEchoChar('*');//hide with '*'
    }
});

The code above shows you a first idea of ​​what you should do. You'll have to use a thread to wait for the desired time.

TheNawaKer
  • 194
  • 1
  • 1
  • 10
  • 2
    As @Andreas suggested in a now deleted comment, "IMHO, OP wants to see the recent character only, like `p`, `*a`, `**s`, `***s`, etc.—similar to what some mobile apps do. From the `JPasswordField` javadoc, this seems not easy to achieve; it most likely requires a new component based on `JTextField`." – trashgod May 21 '14 at 14:09
0

I came across this which may be a good start as it displays the last char entered and shows the rest of the password is masked but doesn't hide it after a set time so you would probably need to implement an event to hide after set time Check it out here

user2991535
  • 119
  • 8
  • Unless I'm missing something? It gives you a starting point for the masked text-box to do what you want minus the last char disappearing after a set point of time. – user2991535 May 24 '14 at 05:51
  • @user2991535 The question was about Java and the JPasswordField Swing component, not about Javascript and html – Andreas Fester May 24 '14 at 20:40