Se voce utilizar o objecto JPasswordField (JPassword foi discontinuado) voce deve utilizar o seguinte codigo:
 
public void actionPerformed(ActionEvent e) {
    JPasswordField input = (JPasswordField)e.getSource();
    char[] password = input.getPassword();
    if (isPasswordCorrect(password)) {
 JOptionPane.showMessageDialog(f,
     "Success! You typed the right password.");
    } else {
 JOptionPane.showMessageDialog(f,
     "Invalid password. Try again.",
     "Error Message",
     JOptionPane.ERROR_MESSAGE);
    }
}
Para maiores detalhes consulte
 
Abaixo um exemplo completo de utilização do JPasswordField:
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class PasswordDemo {
    public static void main(String[] argv) {
        final JFrame f = new JFrame("PasswordDemo");
 
        JLabel label = new JLabel("Enter the password: ");
        JPasswordField passwordField = new JPasswordField(10);
        passwordField.setEchoChar('#');
        passwordField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JPasswordField input = (JPasswordField)e.getSource();
                char[] password = input.getPassword();
                if (isPasswordCorrect(password)) {
                    JOptionPane.showMessageDialog(f,
                        "Success! You typed the right password.");
                } else {
                    JOptionPane.showMessageDialog(f,
                        "Invalid password. Try again.",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                }
            }
        });
 
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        contentPane.add(label, BorderLayout.WEST);
        contentPane.add(passwordField, BorderLayout.CENTER);
 
        f.setContentPane(contentPane);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) { System.exit(0); }
        });
        f.pack();
        f.setVisible(true);
    }
 
    private static boolean isPasswordCorrect(char[] input) {
        char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };
        if (input.length != correctPassword.length)
            return false;
        for (int i = 0;  i < input.length; i ++)
            if (input[i] != correctPassword[i])
                return false;
        return true;
    }
}
 
sds
Marco Zerbini
----
 
- Original Message -----
Sent: Monday, October 22, 2001 5:18 PM
Subject: [java-list] Comparacao JPasswordField com String....

     Estou fazendo uma tela de senha que permite o usuario entrar num objeto JPassword com uma senha  123 e gostaria de compara-la numa variavel criada para este fim, so que ele naum reconhece o if e cai no esle e exibe mensagem.. alguem sabe como fazer esta comparacao...
 

String pw = new String(pf.getPassword());

String aux = "123";

if (pw == aux)

     { sis = new Menu();

      sis.show();

}

else //Exibe mensagem e retorna para Senha

{ JOptionPane.showMessageDialog (this,"SENHA INCORRETA. CONSULTE SEU ADMINISTRADOR" );

setTitle ("Senha Incorreta.");

return;

}

Responder a