Reputation: 43
I'm creating a login window, and the examples I found are having difficulty implementing because they don't show how to move on to the next window after trying to log in. Because It does not tell me how to close Login window and then opens other process's window after "login done" or "Confirm to login" message.
I made UI design with IntelliJ and this is my code. I know this code occours Error. because JOptionPane.showMessageDialog() returns void but i tried to get int return. I uesd it Just for example.
I understand that I have to use "JOptionPane.showConfirmDialog" to receive input as an integer type, but I just want to let user know that they just have logged in, so I don't want to show anything other than the OK button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginForm {
private JPanel Login;
private JTextField IDField;
private JPasswordField PasswordField;
private JButton LoginOKBtn;
private JButton btnIDFind;
private JButton btnPWFind;
private JButton btnCreateAccount;
//Test ID and Password
final String testID="user";
final String testPW="pw";
public LoginForm() {
//Login Btn Event
LoginOKBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String ID=IDField.getText();
String PW=new String(PasswordField.getPassword());
System.out.println(ID);
System.out.println(PW);
//when ID and PW is collect then Show Dialog
if (ID.equals(testID) && PW.equals(testPW)){
int loginOKbtnclicked = JOptionPane.showMessageDialog(null, "Succesfully Loged in!", "Info", JOptionPane.YES_OPTION);
if (loginOKbtnclicked == JOptionPane.OK_OPTION){
System.out.println("TODO - Closing Window Code");
}
else{
}
}
else {
JOptionPane.showMessageDialog(null,"WRONG Password or ID","Alert!",JOptionPane.WARNING_MESSAGE);
}
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Login");
frame.setContentPane(new LoginForm().Login);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Upvotes: 0
Views: 1532
Reputation: 31
Just put the new frame after the messagebox. And put the current frame here, do not send it with null.
JOptionPane.showMessageDialog(**null**, "Succesfully Loged in!", "Info", JOptionPane.YES_OPTION);
Then change the visibility of the current frame, or do what you like. You do not need to do check if he clicked the button or not. Since that messagebox has no purpose other than saying "You are in".
Upvotes: 0