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");
}
});
}
}