Re: subclass JPasswordField
markspace wrote:
It would help if you also posted a short driver for your programs, so
that we could see that you were implementing them correctly, or at how
you were trying to implement them.
For example:
public class PasswordTest {
public static void main(String[] args)
{
// Test harness
javax.swing.SwingUtilities.invokeLater( new Runnable() {
public void run()
{
createAndShowGui();
}
} );
}
private static void createAndShowGui()
{
// Test harness
JFrame jf = new JFrame( "Test Password" );
JPasswordField jpwd = new JPasswordField();
TimedPasswordListener tpl = new TimedPasswordListener();
jpwd.getDocument().addDocumentListener( tpl );
jf.add( jpwd );
jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jf.setLocationRelativeTo( null );
jf.pack();
jf.setVisible( true );
}
}
This can be easily added to your existing class just by giving it a
"main" method.
Onward, however...
Mike wrote:
Ok, so my main question now is: How to I get to the JPasswordField that
the DocumentListener is attached to? How do I reference it?
Yes. If only there were some way of storing references to other classes
in your class, it would be easy.
class TimedPasswordListener implements DocumentListener, ActionListener {
Timer timer;
char echoChar;
JPasswordField passwordField;
....
public void showText(DocumentEvent e, int timeOut) {
Document doc = (Document) e.getDocument();
// How do I set the echo char of the JPasswordField
// to '0' here, and how do I restore it again after
passwordField.setEchoChar( (char) 0 );
// timeOut seconds?
....
Hmmm.
If this is meant sarcastically you are not really helping, you're just
being an ass.
For the person reading this thread at a later stage, here is what
marksass means:
class TimedPasswordListener implements DocumentListener, ActionListener {
private Timer timer = new Timer(3000, this);
private char echoChar;
private JPasswordField pwf;
public TimedPasswordListener(JPasswordField jp)
{
pwf = jp;
}
public void insertUpdate(DocumentEvent e) {
showText(e, 3);
}
public void removeUpdate(DocumentEvent e) {
showText(e, 3);
}
public void changedUpdate(DocumentEvent e) {
// Plain text components do not fire these events
}
public void showText(DocumentEvent e, int timeOut) {
if (0 != pwf.getEchoChar())
echoChar = pwf.getEchoChar();
pwf.setEchoChar((char)0);
timer.stop();
timer.setDelay(timeOut * 1000);
timer.setInitialDelay(timeOut * 1000);
timer.setRepeats(false);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
pwf.setEchoChar(echoChar);
}
}
Then add the listener like this:
JPasswordField jp = new JPasswordField();
jp.getDocument().addDocumentListener(new TimedPasswordListener(jp));
Using this method I wasn't able to clear the password box if the user
starts typing again after the 3 seconds, so I went for the subclass
method after all.
My password box now shows normal characters when typing. Then after 3
seconds of no input, the characters are masked. If you now start typing
again the box will clear all text and you can re-enter something (but
never see the masked password).
Works perfectly, thanks for the pointers (but no thanks for the
arrogance mark).