-3

Something weird is going with my code.

I have a small form (not designed well, but will do for now). The user type username and password, and then press submit.

I added an action listener method for the button. At first, the only thing in it was:

dispose();

and indeed, when pressed, the window closed.

Now when I add an if statement like you see in the code, it does nothing if the value is the correct, however, if it is wrong, it does what I wrote in the else part....

if I am trying to add another condition with && and the passwordfield, it give me error message: the method is deprecated, and Eclipse puts a line over the getText method for the password...

I don't get it, how can it work without an 'if', doesn't work with 'if', and give me warning with &&....thanks...

package HR;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SignIn extends JFrame
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public SignIn()
    {
        this.setTitle("HR SYSTEM LOGIN SCREEN");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 308, 179);

        JPanel contentPane = new JPanel(new GridLayout(3,3,1,15));
        this.getContentPane().add(contentPane);

        JLabel userName = new JLabel("User Name");
        contentPane.add(userName);

        JLabel spaces2 = new JLabel("");
        contentPane.add(spaces2);

        final JFormattedTextField userText = new JFormattedTextField();
        contentPane.add(userText);

        JLabel password = new JLabel("Password");
        contentPane.add(password);

        JLabel spaces3 = new JLabel("");
        contentPane.add(spaces3);

        final JPasswordField passwordText = new JPasswordField();
        contentPane.add(passwordText);

        JLabel spaces1 = new JLabel("");
        contentPane.add(spaces1);

        JButton signButton = new JButton("Sign In");
        contentPane.add(signButton);

        JLabel spaces4 = new JLabel("");
        contentPane.add(spaces4);

        signButton.addActionListener(new ActionListener() 
        {

            @Override
            public void actionPerformed(ActionEvent e) 
            {

                if (userText.getText()=="ABC")
                {
                    dispose();
                }
                else userText.setText("ABC");
            }
        });


    }

}
user3275222
  • 225
  • 3
  • 12

1 Answers1

2

You are comparing strings with == instead of equals()

if (userText.getText().equals("ABC") {
    ...
}
SimonPJ
  • 756
  • 9
  • 21
  • Right, thank you, it did work, however, when I did this: if (userText.getText().equals("ABC") && passwordText.getText().equals("1234")) eclipse gave me this warning message again and put a line over the getText method of the password – user3275222 Feb 11 '14 at 15:39
  • Yeah that's right. While I was through OOP Basics Java course, the instructor told us never to compare string method with anything else but equals() method. +1 – msmolcic Feb 11 '14 at 15:41
  • 1
    You need to use `getPassword()`. Check it out [here](http://docs.oracle.com/javase/7/docs/api/javax/swing/JPasswordField.html). Always check documentation. – takendarkk Feb 11 '14 at 15:43
  • 2
    @user3275222 the warning is a possible NullPointerException warning. To fix, it should be `if ("ABC".equals(userText.getText()))` – Paul Samsotha Feb 11 '14 at 15:48