I'm trying to write a class that allows a user to enter his user-name and password. I'm using the Swing class to create a JFrame- which includes a JButton that will be pressed after the user has entered his information. I want to return the information the user has entered, and I'm using two global variables, which are set in the actionPerformed method to do that. Unfortunately, since this method is void (I'm inheriting it) I need to also include 2 accessor methods. Here is the code for this class:
import javax.swing.*;
import java.awt.*;
import javax.swing.border.Border;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LoginPage extends JFrame
implements ActionListener{
private JButton button;
private JTextField usrEntry;
private JPasswordField pwordEntry;
private String _usrName;
private String _password;
public LoginPage(){
// set fields
_usrName = "";
_password = "";
this.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
this.setSize(700, 700);
this.setBackground(Color.blue);
// make background panel
JPanel background = new JPanel
(new GridBagLayout());
background.setPreferredSize(this.getSize());
background.setBackground(Color.blue);
// make panel to hold contents
JPanel panel = new JPanel();
panel.setBackground(Color.white);
panel.setPreferredSize(new Dimension(300, 200));
// add labels to create some space
JLabel labelHolder1 = new JLabel
(" ");
JLabel labelHolder2 = new JLabel
(" ");
JLabel labelHolder3 = new JLabel(" ");
labelHolder1.setBounds(10, 20, 80, 25);
labelHolder2.setBounds(10, 20, 80, 25);
labelHolder3.setBounds(10, 20, 80, 25);
panel.add(labelHolder1);
panel.add(labelHolder2);
panel.add(labelHolder3);
// add 'user' tag to the panel
JLabel usrLabel = new JLabel("UserName:");
usrLabel.setBounds(10, 20, 80, 25);
panel.add(usrLabel);
// add space for user to enter his username
usrEntry = new JTextField(20);
usrEntry.setBounds(100, 20, 165, 25);
panel.add(usrEntry);
// add 'password' tag to the panel
JLabel pwordLabel = new JLabel("Password:");
pwordLabel.setBounds(10, 50, 80, 25);
panel.add(pwordLabel);
// add space for user to enter his password
pwordEntry = new JPasswordField(20);
pwordEntry.setBounds(100, 50, 165, 25);
panel.add(pwordEntry);
// add button for login
button = new JButton("Log-In");
button.setBounds(10, 80, 80, 25);
button.addActionListener(this);
panel.add(button);
// make the frame
this.setVisible(true);
background.add(panel);
this.add(background);
}
@ Override
public void actionPerformed(ActionEvent press){
if(press.getSource() == button){
_usrName = usrEntry.getText();
_password = pwordEntry.getText();}
System.out.print(_usrName "\n"
_password);
}
// returns user input for userName
public String getUserName(){
return _usrName;
}
// returns the user input for password
public String getPassword(){
return _password;}
}
The problem is, if I instantiate this class and run it, the main method program will finish before the actionPerformed method is even called. This is problematic because, as mentioned, I'm trying to get the values that were entered and return them. Here is a sample main program, and its output:
public class LoginTester{
public static void main(String[] args){
LoginPage login = new LoginPage();
int i = 1 2;
System.out.println(i);
System.out.println(login.getUserName() "\n"
login.getPassword()); } }
Here is the output if I input "UserName" is the first textbox and "Password" in the second:
3
UserName Password
So, as you can see, it print '3' from the main method, then empty String since they are initialized in the Constructor. Then 'UserName' and 'Password' are printed from the actionPerformed method. A successful entry would have printed 'UserName' and 'Password' instead of empty Strings.
Hopefully this is clear enough, if not, I will try to clarify in the comments. Thanks for taking the time to read my problem.
CodePudding user response:
Another idea which might be useful for you: if you want some code to be executed, you can add a windowListener to be called on close, like this:
public class LoginTester{
public static void main(String[] args){
LoginPage login = new LoginPage();
login.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
int i = 1 2;
System.out.println(i);
System.out.println(login.getUserName() "\n"
login.getPassword());
}
});
}
}
This will make the code be executed on closing the window, assuming that is what you want
CodePudding user response:
I am not sure wether i understand your problem.
So actionPerformed
is executed and prints some output, but the println call in the main method will not.
This is because the main method will get executed all at once. If you want to do something at a later point, you will have to use other events, similar to actionPerformed
CodePudding user response:
You might want to use a JDialog which can be set modal. Which means the programm processes after the dialog is closed. https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html