I have two JFrames login.java
and account.java
I need to get the username
from the login.java
page and put it in a variable in the account.java
JFrame
. How can I do this in the Java NetBeans using the Swing?

- 5,141
- 5
- 38
- 59

- 81
- 1
- 2
- 4
-
3[`PasswordDemo`](http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html#eg) is a good example that uses a dialog instead of a second frame. – trashgod Mar 23 '12 at 06:29
-
1*"from one JFrame to Another JFrame.."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) *"in Netbeans"* The same way as using Eclipse, ..or IntelliJ, ..or Notepad. It is Java you need to understand at this point, not your IDE. – Andrew Thompson Mar 24 '12 at 11:19
-
Are you beginner in java and you are confused with NetBeans' GUI Builder, If yes then this post can help you - [Passing Value from one Form to Another in NetBeans](http://www.thepcwizard.in/2012/07/pass-value-from-form-to-another-in.html) – ThePCWizard Jul 16 '12 at 11:20
5 Answers
Instead of Using JFrames for passing values between different forms you can use CardLayout which will persist your data which you have entered in the previous form. All you have to do is Create a JFrameForm and add panels to it.

- 123
- 2
- 11
You can use getter and setter methods...
Set the username in a setter. And using object of login.java use it in account.java through 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 of login.java access getPassword()
, getUsername()
in account.java.
you need to pass object of login.java to account.java first...
-
6Just a simple suggestion, since it's dealing with password, `JPasswordField`'s method `getPassword()` returns an Array of char, so instead of changing it to `String`, better keep it as is. It is done that way for a purpose :-) – nIcE cOw Mar 23 '12 at 07:49
-
@nIcEcOw I feel like it doesn't really help much if you don't mention the purpose. Which is: after you are done checking the array, iterate over it and overwrite all values. This way people won't be able to read it when dumping the memory – Neuron Apr 22 '18 at 05:07
Since you have asked how to pass a variable value from one JFrame to Another JFrame (using swing). So for this put one textbox(tx) and a button(jButton3) in login.java and one label(lx) in account.java where we will print the value of the textbox from login.java .
Type this in login.java :-
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String msg= tx.getText();
new NewJFrame2(msg).setVisible(true);
}
Then overload constructor in account.java :-
public NewJFrame2(String abc ){
initComponents();
lx.setText(abc);
}

- 51
- 1
- 9
Well you have very nice way to do it.
Define new Static Final Objects of that class. and Save that value into the Object.
and in other Class u can easily use that objects and as well as that values. By using
CLASSNAME.OBJECT VALUE.
use that.

- 3,429
- 5
- 34
- 68
The 100% working solution. Suppose u r calling welcome.java
Account ac= new Account(new JFrame(), true);
After this line call a method of welcome.java which u have to create like:
wc.setUser(username);
For account.java
create a method:void setUser(String username) {
user1 = user;
cname.setText(user1);
}
User1 is global variable and available for all which u have to define lke:
String user1;
after it is assigning the username value to user1. here cname is a label which name is cname; so, we are seeting the text of cname to the user.

- 3,375
- 26
- 39