-2

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tobi
  • 1
  • 1
  • The error message tells you the problem: `variable passText of type String`. A **String** does not have a method by that name. – camickr Nov 10 '17 at 15:41

1 Answers1

1

passText is a String and has no method setEchoChar. It must be passField.setEchoChar('*')

private class CheckBoxListener implements ItemListener
  {
     public void itemStateChanged(ItemEvent e)
     {
        String passText = passField.getText();

        if (e.getSource() == passCheck)
           passField.setEchoChar('*');
        else
           passField.setEchoChar((char)0); 
     }

   }

UPDATE:

Your Field must be an JPasswordField:

From the documentation Of JTextField:

The method setEchoChar and getEchoChar are not provided directly to avoid a new implementation of a pluggable look-and-feel inadvertently exposing password characters. To provide password-like services a separate class JPasswordField extends JTextField to provide this service with an independently pluggable look-and-feel.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • When I change it to that I get similar errors. `Login.java:150: error: cannot find symbol passField.setEchoChar('*'); ^ symbol: method setEchoChar(char) location: variable passField of type JTextField Login.java:152: error: cannot find symbol passField.setEchoChar((char)0); ^ symbol: method setEchoChar(char) location: variable passField of type JTextField 2 errors` – Tobi Nov 10 '17 at 15:44
  • @Tobi: See my updated answer – Jens Nov 10 '17 at 15:48