I get an error "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JPasswordField
at def3.password$MyActionListener.actionPerformed(password.java:103)"
in the following code, what am I doing wrong?
static class MyActionListener implements ActionListener {
private String userInput = "username";
private static final String passText = "password" ;
JFrame frame ;
MyActionListener(JFrame f){
frame = f;
}
public void actionPerformed(ActionEvent e) {
//JTextField userInput = (JTextField) e.getSource();
>>> JPasswordField passInput = (JPasswordField) e.getSource(); <<<
//String username = userInput.getText();
char[] pass = passInput.getPassword();
String password = new String(pass);
if (/*username.equals(loginText) &&*/ password.equals(passText)) {
JOptionPane.showMessageDialog(null, "Corerect");
frame.getContentPane().setBackground(Color.blue);
}
else {
JOptionPane.showMessageDialog(null, "Wrong Login Details");
//frame.getContentPane().setBackground(Color.red);
}
}