Issue
I am trying to use setText function in netbeans. So there is a login page if the user login to the page with the correct credentials, (in this case that is only admin) in the next page (which is the main page) there should be a message with Welcome User I am trying to change the label to Welcome user in the main page
Backend of the login button
private void loginMouseClicked(java.awt.event.MouseEvent evt) {
if(loginUsername.getText().toString().trim().length() == 0 && loginPassword.getText().toString().trim().length() == 0){
int opt = JOptionPane.showConfirmDialog(null, "All fields are required", "Alert", JOptionPane.CLOSED_OPTION);
}
else if((loginUsername.getText().toString().equals("admin") && loginPassword.getText().toString().equals("admin"))){
new MainPage().setVisible(true);
this.setVisible(false);
}
else{
int opt = JOptionPane.showConfirmDialog(null, "Username or Password Incorrect", "Alert", JOptionPane.CLOSED_OPTION);
}
}
Code that i tried in the Main Page
SignUp signUp = new SignUp();
MainPage mainPage = new MainPage();
mainPage.setVisible(true);
mainPage.jLabel2.setText("Welcome" + signUp.loginUsername.getText().toString());
Solution
Don't use a MouseListener. You should add an
ActionListner
to the button on your login page. TheActionListener
will be invoked when you click on your Login button.The second block of code you posted should be in the
ActionListener
code of your login button.
So the code should be something like:
else if((loginUsername.getText().equals("admin") && loginPassword.getText().equals("admin")))
{
MainPage mainPage = new MainPage();
mainPage.jLabel2.setText("Welcome" + loginUsername.getText());
mainPage.setVisible(true);
this.setVisible(false);
}
Answered By - camickr