Reputation: 819
I'm currently developing a login form for a chat program and want the program to load the frame and wait for the userinput. Unforunately the program opens the frame, but at the same time resumes the main method. I hope you have some ideas to help me.
Greetings
public static void main(String[] args){
boolean running = true;
//Starting JFrame
chatFrame.loginFrame();
//Processing - Receiving Status from Login method
if(getStatus() == 1){
...
} else {
System.out.println("An Error occured..");
System.exit(0);
}
}
JFrame Class:
public class chatFrame{
private static String sLogin;
private static String password;
public static void loginFrame(){
System.out.println("Launching Frame");
JFrame loginFrame = new JFrame();
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField user = new JTextField("Username");
JTextField pass = new JTextField("Password");
JButton login = new JButton("Login");
loginFrame.setLayout(new BorderLayout());
loginFrame.add(user, BorderLayout.NORTH);
loginFrame.add(pass, BorderLayout.CENTER);
loginFrame.add(login, BorderLayout.SOUTH);
loginFrame.pack();
loginFrame.setSize(250, 150);
loginFrame.setVisible(true);
login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Action performed");
String sLogin = user.getText();
String password = pass.getText();
//Calling Login method
ClEngine.login(sLogin, password);
System.out.println("dataIn:" + dataIn);
loginFrame.setVisible(false);
}
});
}
}
Upvotes: 0
Views: 251
Reputation: 4385
Your desire is to await response to the user finishing their working with an application window, and there are several possible ways to solve this. The easiest, and the one I recommend is your making your login window a modal JDialog, not a JFrame. This way the calling code's program flow will halt while the dialog is visible, and once it has been closed, the calling program's code flow will resume, and then it can query the dialog's state and find out if data has been entered, and if it's valid data.
Another option is to allow outside classes to add listeners to the login window, such as an ActionListener to its button or a WindowListener, but this is a little more complicated to handle.
For example
import java.awt.Window;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
public class ChatLogin extends JPanel {
private static JDialog dialog;
private static ChatLogin chatLogin;
private static boolean loginValid;
private JTextField userNameField = new JTextField(10);
private JPasswordField passwordField = new JPasswordField(10);
public ChatLogin() {
JPanel inputPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(2, 10, 2, 2);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
inputPanel.add(new JLabel("User Name:"), gbc);
gbc.gridy = 1;
inputPanel.add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(2, 2, 2, 2);
inputPanel.add(userNameField, gbc);
gbc.gridy = 1;
inputPanel.add(passwordField, gbc);
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 2, 2));
btnPanel.add(new JButton(new LoginAction()));
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}
public String getUserName() {
return userNameField.getText();
}
public char[] getPassword() {
return passwordField.getPassword();
}
public static boolean isLoginValid() {
return loginValid;
}
private class LoginAction extends AbstractAction {
public LoginAction() {
super("Login");
putValue(MNEMONIC_KEY, KeyEvent.VK_L);
}
@Override
public void actionPerformed(ActionEvent e) {
loginValid = true;
Window win = SwingUtilities.getWindowAncestor(ChatLogin.this);
win.dispose();
}
}
public static JDialog getInstance(Window win, String title) {
dialog = new JDialog(win, title, ModalityType.APPLICATION_MODAL) {
@Override
public void setVisible(boolean b) {
loginValid = false;
super.setVisible(b);
}
};
chatLogin = new ChatLogin();
dialog.add(chatLogin);
dialog.pack();
dialog.setLocationRelativeTo(win);
return dialog;
}
public static JDialog getInstance() {
if (dialog == null) {
return getInstance(null, "Login");
} else {
return dialog;
}
}
public static ChatLogin getChatLoginInstance() {
return chatLogin;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
ChatLogin.getInstance().setVisible(true);
if (ChatLogin.isLoginValid()) {
ChatLogin chatLogin = ChatLogin.getChatLoginInstance();
String userName = chatLogin.getUserName();
String password = new String(chatLogin.getPassword()); // not a safe thing to do
// here test that user name and password are valid
System.out.println("user name: " + userName);
System.out.println("Password: " + password);
}
});
}
}
Upvotes: 2
Reputation: 48258
this is happening because you are opening the frame by calling
chatFrame.loginFrame();
but the program continues, then exits the main method and ends the application...
use callbacks to verify the user events...
Upvotes: 0