How to pass variable value from one JFrame to another JFrame in Netbeans

I have two JFrames


I need to get username

from a page

and put it in a variable in


. How do I do this in Java NetBeans using Swing?


source to share

5 answers

Instead of using JFrames to pass values ​​between different forms, you can use CardLayout, which will save your data that you entered in the previous form. All you have to do is create a JFrameForm and add panels to it.



You can use getter and setter methods ...

Set the username in the setter. And using object, use it in 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 getPassword()

, getUsername()

in you need to pass object to first ...



Ok, you have a very nice way to do it.

Define new static end objects of this class. and store this value in an object.

and in another class u can easily use these objects as well as these values. Using


use this.



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 and one label (lx) in where we will print the textbox value from

Enter this in -

 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      String msg= tx.getText();
      new NewJFrame2(msg).setVisible(true);


Then overload the constructor in -

public NewJFrame2(String abc ){




100% working solution. Suppose ur calls

Account ac= new Account(new JFrame(), true);


After this line, call the method, which you need to create as:




create a method:void setUser(String username) {
        user1 = user; 


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.



All Articles