Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4182961

JPasswordField (swing-1.1.b3), once set cannot be cleared

XMLWordPrintable



      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)
      ======================================================================

            apikalev Andrey Pikalev
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: