I seem to be getting 2 errors when trying to compile this Login GUI.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame
{
final int USER_CONS = 8;
final int PASS_CONS = 8;
int ATTEMPTS = 3;
private JCheckBox userCheck;
private JCheckBox passCheck;
private JButton login;
private JButton exit;
private JLabel userLabel;
private JLabel passLabel;
private JLabel attemptsLabel;
private JTextField userField;
private JTextField passField;
private JTextField attemptsField;
public static void main (String[] args)
{
new Login();
}
public Login()
{
setTitle("Login Screen");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
BuildPanel();
}
public void BuildPanel()
{
JPanel panel = new JPanel();
JPanel panelNorth = new JPanel();
JPanel panelSouth = new JPanel();
JPanel panelEast = new JPanel();
JPanel panelWest = new JPanel();
JPanel panelCenter = new JPanel();
add(panelNorth, BorderLayout.NORTH);
add(panelSouth, BorderLayout.SOUTH);
add(panelEast, BorderLayout.EAST);
add(panelWest, BorderLayout.WEST);
add(panelCenter, BorderLayout.CENTER);
userLabel = new JLabel("Username");
userField = new JTextField(USER_CONS);
userCheck = new JCheckBox();
passLabel = new JLabel ("Password");
passField = new JTextField(PASS_CONS);
passCheck = new JCheckBox();
attemptsLabel = new JLabel("Max attempts");
attemptsField = new JTextField(4);
JButton login = new JButton("Login");
JButton exit = new JButton("Exit");
exit.addActionListener(new ButtonListener());
login.addActionListener(new ButtonListener());
passCheck.addItemListener(new CheckBoxListener());
panelSouth.add(login, BorderLayout.EAST);
panelSouth.add(exit, BorderLayout.WEST);
panelCenter.add(userLabel);
panelCenter.add(userField);
panelCenter.add(userCheck);
panelCenter.add(passLabel);
panelCenter.add(passField);
panelCenter.add(passCheck);
panelWest.add(attemptsLabel);
panelWest.add(attemptsField);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent closing)
{
String actionCommand = closing.getActionCommand();
String userText = userField.getText();
String passText = passField.getText();
attemptsField.setText(String.valueOf(ATTEMPTS));
if (actionCommand.equals("Exit"))
{
System.exit(0);
}
else if (userText.length() > USER_CONS || passText.length() > PASS_CONS)
{
--ATTEMPTS;
JOptionPane.showMessageDialog(null,"Invalid login. Try again.");
if( ATTEMPTS == -1)
{
JOptionPane.showMessageDialog(null,"You have no more attempts. Goodbye.");
System.exit(0);
}
}
else if (userText.length() <= USER_CONS || passText.length() <= PASS_CONS)
{
JOptionPane.showMessageDialog(null,"You have successfully logged in.");
}
}
}
private class CheckBoxListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
String passText = passField.getText();
if (e.getSource() == passCheck)
passText.setEchoChar('*');
else
passText.setEchoChar((char)0);
}
}
}
The errors lie in my private class CheckBoxListener. I am trying to make it so that if a checkbox is selected (i.e the hide password in this case), it would make every character in the text field a *. But for some reason I get the errors,
Login.java:150: error: cannot find symbol
passText.setEchoChar('*');
^
symbol: method setEchoChar(char)
location: variable passText of type String
Login.java:152: error: cannot find symbol
passText.setEchoChar((char)0);
^
symbol: method setEchoChar(char)
location: variable passText of type String
2 errors