Re: subclass JPasswordField
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.