I'm trying to create a login form and I have some problems at getting the showPassword checkBox work. When showPassword is selected I want the content of the JPasswordField, passwordField, to be visible and when showPassword is not selected I want it to be "hide". I don't understant why my code doesn't work. I write the code this way because I want to implement it in the future as a Model View Controller. I'd prefer not to change any attribute from private in public if possible. Any ideas why this doesn't work? Thanks!
package project3;
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class LogInWindow extends JFrame {
private Container container = getContentPane();
private JLabel titleLabel = new JLabel("WarehouseApp");
private JLabel userLabel = new JLabel("USERNAME");
private JLabel passwordLabel = new JLabel("PASSWORD");
private JTextField userTextField = new JTextField();
private JPasswordField passwordField = new JPasswordField();
private JButton loginButton = new JButton("LOGIN");
private JCheckBox showPassword = new JCheckBox("Show Password");
private JLabel logInAsLabel = new JLabel("LOGIN AS");
private JComboBox<String> logInAsComboBox = new JComboBox<String>();
public LogInWindow() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(10, 10, 370, 370);
this.setName("Login Form");
this.setResizable(false);
this.setLocationRelativeTo(null);
container.setLayout(null);
titleLabel.setBounds(80, -10, 200, 100);
userLabel.setBounds(50, 80, 100, 30);
userTextField.setBounds(150, 80, 150, 30);
passwordLabel.setBounds(50, 130, 100, 30);
passwordField.setBounds(150, 130, 150, 30);
logInAsLabel.setBounds(50, 180, 100, 30);
logInAsComboBox.setBounds(150, 180, 150, 30);
showPassword.setBounds(150, 220, 150, 30);
loginButton.setBounds(150, 260, 100, 30);
Font font = new Font("Times New Roman", Font.BOLD, 30);
titleLabel.setFont(font);
logInAsComboBox.addItem("ADMIN");
logInAsComboBox.addItem("CLIENT");
logInAsComboBox.setSelectedIndex(-1);
container.add(titleLabel);
container.add(userLabel);
container.add(passwordLabel);
container.add(userTextField);
container.add(passwordField);
container.add(logInAsLabel);
container.add(logInAsComboBox);
container.add(showPassword);
container.add(loginButton);
}
public void showPasswordWhenClicked(ActionListener listenForPassword) {
showPassword.addActionListener(listenForPassword);
}
public boolean getPasswordStatus() {
if (showPassword.isSelected()==true)
return true;
return false;
}
public void setPasswordVisible() {
passwordField.setEchoChar((char) 0);
}
public void setPasswordInvisible() {
passwordField.setEchoChar('*');
}
}
package project3;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller {
private LogInWindow theView;
public Controller(LogInWindow theView) {
this.theView = theView;
this.theView.showPasswordWhenClicked(new showPasswordListener());
}
public class showPasswordListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (theView.getPasswordStatus()==true) {
theView.setPasswordVisible();
} else {
theView.setPasswordInvisible();
}
}
}
public static void main(String[] args) {
LogInWindow logIn = new LogInWindow();
logIn.setVisible(true);
}
}
Your code does not create an instance of Controller
, so that class’s constructor is never called. Therefore, showPasswordWhenClicked
is never called.
Try adding this line to your main
method:
new Controller(logIn);