so I'm new to swing on java and I'm trying to make a login window as a precursor to a program. The login window is quite standard, it has a password and user input, a login button, and an x on the top right. However, I'm trying to make it so that the user cannot interact with the main program until they've inputted the correct login data. However, while the setenabled(false) function has allowed me to prevent access to the main program, after the user enters the correct info, nothing happens and the login screen remains.
public static void main(String[] args) {
JFrame frame = MainUI.getInstance();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(900, 600);
frame.pack();
frame.setVisible(true);
frame.setEnabled(false);
login loginframe = new login();
loginframe.setResizable(false);
loginframe.setTitle("Login Form");
loginframe.setVisible(true);
loginframe.toFront();
loginframe.setBounds(10, 10, 375, 300);
loginframe.setDefaultCloseOperation(loginframe.EXIT_ON_CLOSE);
if (loginframe.loggedin)
frame.setEnabled(true);
}
in my login class I have an loggedin boolean that is true if the right input is detected. however, the setenabled(true) doesn't seem to be executing, and the login window doesn't seem to be closing after the right input is entered. This is my actionperformed method from my login class:
public void actionPerformed(ActionEvent e){
//Coding Part of LOGIN button
if (e.getSource() == loginButton) {
String userText;
String pwdText;
userText = userTextField.getText();
pwdText = passwordField.getText();
String line;
Boolean match = false;
try {
BufferedReader br = new BufferedReader(new FileReader("database.txt"));
while((line = br.readLine())!= null){
String[] values = line.split(",");
//System.out.println(values[0] values[1]);
if (userText.equalsIgnoreCase(values[0]) && pwdText.equalsIgnoreCase(values[1])) {
match = true;
break;
} else {
match = false;
}
}
if (match) {
JOptionPane.showMessageDialog(this, "Login Successful");
loggedin= true;
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
} else {
JOptionPane.showMessageDialog(this, "Invalid Username or Password");
loggedin = false;
System.exit(0);
}
} catch (IOException a) {
a.printStackTrace();
}
}
}
Does anyone know why this is? Is my this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); not doing what I think it should be doing? Does it not just close the current window (the login window) thus giving the user access to the main program?
CodePudding user response:
package stackoverflow;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class ExitFrame {
public static void main(final String[] args) {
final JFrame f = new JFrame();
// this will do nothing, thus not react to the user trying to close the window
f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
// this will only hide the window, as if fsetVisible(false) was called
f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
// this will actually dispose the window, as if f.dispose() was called
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
// this will shut down the whole JVM, ending your program, as if System.exit(0); was called
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
To help you on your actual problem:
For blocking access to the calling (parent) Window, you should use Dialogs, like the JDialog, set it to modal mode, and give the parent window was parameter.
final JDialog d = new JDialog(f);
d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
d.setModal(true);
// add elements
d.setVisible(true); // will block until d gets hidden or disposed.
// code will resume here
You can also just have class MyDialog extends JDialog
, like you do with JFrame in your code.
An even more advanced method
is not to implement stuff on JFrames or JDialogs, but simply as JPanel. Why? Because you then can add it anywhere you like, and dont have to worry about the 'external' behaviour.
You can then add that Panel to a JFrame, JWindow, JDialog, another JPanel, and even the JOptionPane with message dialogs etc.
It is a bit harder to implement, because you need to attach the 'external' events of the parent to the JPanel somehow, but once you figure that out it's easy as pie.