JDK-4182961 : JPasswordField (swing-1.1.b3), once set cannot be cleared
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1998-10-20
  • Updated: 2001-04-25
  • Resolved: 2001-04-25
Related Reports
Duplicate :  
Description

Name: tb29552			Date: 10/20/98


/*

1) Run the test program below, on the first go
everything works fine.

On second and subsequent attempts (ie user types
bad password on the first try) the JPasswordField
works fine if a mouse click is used to put focus
into the Password: field.  If the user hits <TAB>,
focus moves to the Password: field, but the cursor
lands in the middle.  If you go enter some text
and click [OK] anyway, the getPassword() method
returns an empty string.

I don't thinks it likes the setText("") call to
clear the field.  Unfortunately i cannot find
another way to clear the existing field.

2)
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class testLogin extends JDialog implements ActionListener {
    JTextField user = new JTextField(12);
    JPasswordField pass = new JPasswordField(12);
    JButton ok = new JButton("OK");
    JButton cancel = new JButton("Cancel");
    boolean valid = false;

    testLogin (Frame f) {
        super(f, "Login", true);

        ok.setMnemonic('O');
        cancel.setMnemonic('C');

        user.addActionListener(this);
        pass.addActionListener(this);
        ok.addActionListener(this);
        cancel.addActionListener(this);

        Container c = getContentPane();
        c.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.ipadx = 10;
        gbc.ipady = 5;
        gbc.insets = new Insets(10, 10, 10, 10);

        gbc.gridwidth = GridBagConstraints.RELATIVE;
        c.add(new JLabel("Username:", JLabel.RIGHT), gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        c.add(user, gbc);
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        c.add(new JLabel("Password:", JLabel.RIGHT), gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        c.add(pass, gbc);
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        c.add(ok, gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        c.add(cancel, gbc);
    }

    public boolean doLogin() {
        pack();
        Dimension mysize = getSize();
        Dimension scrsize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((scrsize.width - mysize.width) / 2, (scrsize.height - mysize.height) / 2);
        pass.setText("");
        user.requestFocus();
        setVisible(true);
        return valid;
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == user) {
            pass.requestFocus();
        } else if (source == pass || source == ok) {
            valid = true;
            setVisible(false);
        } else {
            valid = false;
            setVisible(false);
        }
    }

    public static void main(String[] a) {
        testLogin tl = new testLogin (new Frame());
        //int logincount = 3;
        int logincount = 1024;
        while (logincount > 0 && tl.doLogin()) {
            System.out.println("User:" + tl.user.getText());
            System.out.println("Password:" + new String(tl.pass.getPassword()));
            logincount--;
        }

    }

}
/*

3) No errors reported, it just didn't work

4) As above

5) Occurs on Window NT4.0SP3, tried different look & feels all with
the same result Roughly the same code works with previous Swing
releases

 */

(Review ID: 40706)
======================================================================

Comments
EVALUATION The password does get cleared. However, the caret is initially in the wrong place (appears as if there is something in the password field). timothy.prinzing@eng 1998-10-29 Name: apR10133 Date: 04/25/2001 The bug is not reproducible as it reported, but the symptoms described above in the evaluation are reported at the separate bug 4424708. ###@###.### ======================================================================
24-08-2004

WORK AROUND Name: tb29552 Date: 10/20/98 Recreate the JPassword field or the whole login dialog from scratch every time you need one; do not reuse. ====================================================================== Name: apR10133 Date: 04/25/2001 Use updateUI() method to update a password field. ###@###.### ======================================================================
24-08-2004