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

I have two JFrames login.java

and account.java


I need to get username

from a page login.java

and put it in a variable in account.java

JFrame

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

+3


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.



+3


source


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 ...

+2


source


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

The CLASSNAME.OBJECT value.

use this.

0


source


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);
    }

      

0


source


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.

-1


source







All Articles