0

Here is my code and what I want to do is, when the login button is pressed, I would like it to check the password and text fields for the right password(found in the if statement). I need help on what to do to get the input from the text and password fields. When I run the code, it skips right to the else statement and does the code in there, I want it to do the if statement as I have entered the right username and password. I was trying to figure out how to get input from the text fields but I don't know how to. I would appreciate some help, thank you.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.TextArea;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseListener;

    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;

    public class GUI_2 implements ActionListener {

        private JLabel PassLabel;
        private JFrame frame;
        private JButton enterButton;
        private JLabel UserLabel;
        private JLabel label;
        private JPanel panel;
        private JFrame Incorrect;
        private JTextField password;
        private JTextField username;
        private String rightPassword;
        private String rightUsername;
        private String passwordInput;
        private String usernameInput;
        private JButton UsernameEnter;
        private JButton PasswordEnter;
        private final static String newline = "\n";
        private TextArea textArea;


        public GUI_2()
        {
            PassLabel = new JLabel("Enter Password:");
            password = new JPasswordField(11);
            UserLabel = new JLabel("Enter Username:");
            username = new JTextField(11);
            enterButton = new JButton("Login");
            label = new JLabel("Access");
            UsernameEnter = new JButton("Enter");
            PasswordEnter = new JButton("Enter");
            frame = new JFrame();
            panel = new JPanel();
            Incorrect = new JFrame();
            enterButton.addActionListener(this);
    
            panel.setBorder(BorderFactory.createEmptyBorder(200,200,60,300));
            panel.setLayout(new GridLayout(5, 5));
            panel.add(UserLabel);
            panel.add(username);
            panel.add(PassLabel);
            panel.add(password);
            panel.add(label);
            panel.add(enterButton);
    
            frame.add(panel, BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Password Login");
            frame.pack();
            frame.setVisible(true);
    
    
        }


        public static void main(String[] args) {
            new GUI_2();
            // TODO Auto-generated method stub

        }






    public void actionPerformed(ActionEvent arg0)
    {
    
    
        if (passwordInput == "password" && usernameInput == "harry")
        {
            frame.setTitle("Success");
            label.setForeground(Color.green);
            label.setText("Access granted");
        }
        else
        {
            frame.setTitle("Access Denied");
            label.setForeground(Color.red);
            label.setText("Access Denied");
        }
    }
    }
  • 1
    [How to compare Strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Aug 04 '20 at 11:38
  • 1
    Also, you need to get the field's text, not compare the field itself. – RealSkeptic Aug 04 '20 at 11:39
  • ^ thats correct aswell. Before you compare the `passwordInput` and `usernameInput` you actually need to initialize these strings with the correct values. e.g. `passwordInput = password.getText()`. Or you skip this part if you don't need the input stored temporarily and compare these strings directly in your if clause. e.g. `if (password.getText().equals("password") && username.getText().equals("harry"))`. – maloomeister Aug 04 '20 at 11:41
  • @maloomeister they would also need to check the TextFields text is not null before they check if it equals something – Conor Egan Aug 04 '20 at 12:35
  • @ConorEgan that's right. – maloomeister Aug 04 '20 at 12:45
  • 1) The text of a text field might be a 0 length string, but will never be `null` 2) A log-in should be in a dialog or option pane, not a frame. 3) Don't use a text field for a password, it is not secure. Use a `JPassWordField` and never convert the char array to a `String` (that makes it not secure any more). 4) Use meaningful, descriptive class names, so .. `LoginWindow` or similar as opposed to `GUI_2`. 5) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut .. – Andrew Thompson Aug 04 '20 at 14:33
  • .. specifically for formatting code. 6) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 7) `TextArea textArea` should be `JTextArea textArea` 8) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 9) Given 8 comments not related to the immediate problem, you seem to be 'biting off more than you can chew' at this stage. Will leave there. – Andrew Thompson Aug 04 '20 at 14:33

1 Answers1

1

1: you can get the input password with: password.getText()

2: you should use "equals" compare two string passwordInput.equals("password")

so modify the actionPerformed method like this:

    public void actionPerformed(ActionEvent arg0) {

        passwordInput = password.getText();
        usernameInput = username.getText();
        if (passwordInput != null
                && usernameInput != null
                && passwordInput.equals("password")
                && usernameInput.equals("harry")) {
            frame.setTitle("Success");
            label.setForeground(Color.green);
            label.setText("Access granted");
        } else {
            frame.setTitle("Access Denied");
            label.setForeground(Color.red);
            label.setText("Access Denied");
        }
    }
ggbond
  • 120
  • 5