1

How to pass values entered in one JFrame's text field as an input parameter in other JFrame?

Entered user name and password in first JFrame through JTextFields..

String usr = jTextField2.getText();
String pass = jTextField3.getText();

Same username and password should be given as input in forth frame each frame is redirected to other on button click

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Raghu Nandiraju
  • 11
  • 1
  • 1
  • 2
  • You mean passing the username and password to other jtextfield's frames? – Azad Jul 01 '13 at 20:19
  • Hi Azad , Thanks for your response .. :) I don't want the data to be populated in other frames text field.. i want the data as input in the code backend.. – Raghu Nandiraju Jul 01 '13 at 20:25
  • I think, I understood you finally, you mean each frame has a two strings ( user, pass), am I right? – Azad Jul 01 '13 at 20:33
  • yes Azad...The code that i use in second form is getLogger().info("ConnectToDevice using: "+networkElement+" : "+getUsername()+" : "+getPassword()); for which i need values entered in text fields of form1 – Raghu Nandiraju Jul 01 '13 at 20:36
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jul 02 '13 at 03:40

4 Answers4

8

Suppose you have many frames, you have to create instance variables for that purpose. If you don't know what an instance variable see this tutorial. Lets see an example:

This will be your frame that sends the variables :

public class MainFrame {
    public void actionPerformed(ActionEvent ev) {
    String user = userField.getText();
    String pass = passField.getText();
    FrameOne frameOne = new FrameOne();
    frameOne.setUser(user);
    frameOne.setPass(pass);

    /* 
     * You've passed the user and pass to other frame,
     * now you can make it visible.
     */
    frameOne.setVisible(true);
 }

And this will be your first frame:

public class FrameOne extends JFrame {
    private JTextField userField;
    private JTextField passField;

    // then create setters and getter
    public void setUser(String user) {this.userField.setText(user);}
    public String getUser() {return this.userField.getText();}

    public void setPass(String pass) {this.passField.setText(pass);}
    public String getPass() {return this.passField.getText();}

    public FrameOne() {
        //define the components here
    }
}

NOTE : I didn't compile the code, this is only for demonstration on your problem.

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
Azad
  • 5,047
  • 20
  • 38
  • @RaghuNandiraju: You are welcome, this is your first question, So, please consider if the answer were accepted or not. See this question on SO: [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). And see [StackOverFlow/about](http://stackoverflow.com/about) it's very useful. – Azad Jul 01 '13 at 21:00
3

You can also pass values to the constructor like this

Your main frame

public class MainFrame{
      //
      public void actionPerformed(ActionEvent ev){

       FrameOne frameOne = new FrameOne(userField.getText(), passField.getText());

       //you've passed the user and pass to other frame.
       // then you can make it visible.
       frameOne.setVisible(true);
     } 
} 

Your next frame

public class FrameOne extends JFrame{
  private String user;
  private String pass;

  public FrameOne(String usr, String pas){
    this.user=usr;
    this.pass=pas;
    //define the components here
 }
}
Shahrzad
  • 1,062
  • 8
  • 26
0

first create publicly static type variable

public static JTextField txt2; public JTextField txt1,button1;

//action button1 in 1st JFrame

JFrame2.setVisible(true); JFrame2.txt2.setText(Me.txt1.getText());

Saquib Azam
  • 73
  • 1
  • 4
0
Suppose u have two class like this:

for login.java
----------------
suppose u r calling welcome.java:
Welcome wc= new Welcome(new JFrame(), true);
after this line call a method of welcome.java which u have to create like:
wc.setUser(username);

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