How to pass variable value from one JFrame to another JFrame in Netbeans
You can use getter and setter methods ...
Set the username in the setter. And using login.java object, use it in account.java via getter ...
public class login {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = this.usernameTextField.getText();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = this.passwordTextField.getText();
}
}
Using objects access login.java getPassword()
, getUsername()
in account.java. you need to pass login.java object to account.java first ...
source to share
Since you asked how to pass a variable value from one JFrame to another JFrame (using swing). So for this put one text code (tx) and button (jButton3) in login.java and one label (lx) in account.java where we will print the textbox value from login.java.
Enter this in login.java: -
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String msg= tx.getText();
new NewJFrame2(msg).setVisible(true);
}
Then overload the constructor in account.java: -
public NewJFrame2(String abc ){
initComponents();
lx.setText(abc);
}
source to share
100% working solution. Suppose ur calls welcome.java
Account ac= new Account(new JFrame(), true);
After this line, call the welcome.java method, which you need to create as:
wc.setUser(username);
For account.java
create a method:void setUser(String username) {
user1 = user;
cname.setText(user1);
}
User1 is a global variable and is available to everyone, for which you need to define lke:
String user1;
after assigning the user1 value to the user. where cname is the label whose name is cname; so we read the text cname to the user.