I have an annoying message box that pops up when I dont want it.
the problem occurs after a user logs on and the hidden button is visible, but when it is clicked it show the “correct” message again? Ive also tried putting it on top of the first statement and bottom.
EDIT:The annoying message is the message that comes up after a successful login, and no I dont need to spell check,
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("Closing window!");
System.exit(0);
}
}
class LoginForm extends JFrame implements ActionListener {
private final String username = "user";
private final String password = "pass";
JFrame frame;
JPanel jPanel;
JLabel userLabel;
final JTextField userText;
JLabel passLabel;
final JPasswordField passText;
JButton loginBtn;
JButton shopBtn;
JLabel welcome;
{
frame = new JFrame();
jPanel = new JPanel();
userLabel = new JLabel("Login : ");
userText = new JTextField(10);
passLabel = new JLabel("Password : ");
passText = new JPasswordField(10);
loginBtn = new JButton("Login");
shopBtn = new JButton("Go to Shop");
welcome = new JLabel("Welcome to ECSE501 Computers");
setTitle("Login Page");
loginBtn.addActionListener(this);
shopBtn.addActionListener(this);
Container c1 = new Container();
c1.setLayout(new GridLayout (3,2));
c1.add(userLabel);
c1.add(userText);
c1.add(passLabel);
c1.add(passText);
c1.add(loginBtn);
Container c2 = new Container();
c2.setLayout(new BoxLayout(c2, BoxLayout.Y_AXIS));
c2.add(welcome);
c2.add(shopBtn);
welcome.setVisible(false);
shopBtn.setVisible(false);
add(jPanel);
jPanel.add(c1);
jPanel.add(c2);
}
public void actionPerformed(ActionEvent e) {
String userInput = userText.getText();
char[] pass = passText.getPassword();
String p = new String(pass);
if (userInput.equals(username) && p.equals(password)) {
jPanel.setVisible(true);
welcome.setVisible(true);
jPanel.setBackground(Color.green);
JOptionPane.showMessageDialog(null, "Correct");
shopBtn.setVisible(true);
JButton hiddenBtn = (JButton) e.getSource();
if ( hiddenBtn == shopBtn)
{
SelectionForm selection = new SelectionForm();
selection.select();
}
}
else {
jPanel.setBackground(Color.red);
JOptionPane.showMessageDialog(null, "Wrong Login Details");
}
}
}
public class LoginTester {
public static void main(String[] args) {
// register an event handler for frame events
LoginForm frame = new LoginForm();
frame.addWindowListener(new MyWindowListener());
frame.setSize(300, 200);
frame.setVisible(true);
//frame.pack();
}
}
You put this all in your
actionPerformedmethod. That is called whenever any action is performed, a button is clicked, a text field is edited, etc…If you don’t want this to run when you click the button, check for the source of the event, using
getSource(), and if the source is the button, then don’t run the code. Your method would look like this: