0

I have two JFrames named first and second respectively. Both frames have jtextfields to getText() and setText(). I am able to setText from first to second JFrame, but unable setText from second to first.

In first and second frame I have made all JTextFields public static

first.java

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    second sec = new second();  
    sec.jTextField1.setText(this.jTextField1.getText()); // this works
    sec.jTextField2.setText(this.jTextField2.getText()); 
    sec.setVisible(true);

    public static javax.swing.JTextField jTextField1;
    public static javax.swing.JTextField jTextField2;
    private javax.swing.JButton jButton1;

second.java

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   first f = new first();
   f.jTextField1.setText(this.jTextField3.getText()); //not working
   f.jTextField2.setText(this.jTextField4.getText());
   }
   public static javax.swing.JTextField jTextField1;
   public static javax.swing.JTextField jTextField2;
   public static javax.swing.JTextField jTextField3;
   public static javax.swing.JTextField jTextField4;
   private javax.swing.JButton jButton1;

on click of second frames button first frame should get updated. I don't want open first frame again.

Ashish Kudale
  • 1,230
  • 1
  • 27
  • 51

3 Answers3

1

Before I begin, have a look at The Use of Multiple JFrames, Good/Bad Practice?`

"on click of second frames button first frame should get updated. I don't want open first frame again."

It's not working because you're creating a new JFrame (first). Of course it's going to open a new frame. Instead of creating a new frame, pass the same one as a reference.

I'll give you an example using a JDialog instead of a JFrame. If after you read the above link and you still decide a JFrame is what you want, the same code can be implemented into a JFrame. They basicially have the same constructs.

So basically what I mean by passing a reference is this. Pass the JFrame to the constructor of the JDialog. This way the JFrame you pass to it, will be the same referenced JFrame.

public class MyDialog extends JDialog {
    private First frame;
    private JTextField thisTextField;

    public MyDilaog(final First frame, boolean modal) {
        super(frame, modal);
        this.frame = frame;

        thisTextFiled = new JTextField(20);
    }
}

Int your First class just create it like this

MyDialog dialog = new MyDialog(First.this, true);

Now you have a reference to the frame. You also want a getter in the JFrame class that will access the JTextField from the JFrame class. Then you can set its text, as it also is referenced. So you can do something like this from the JDialog

public void actionPerformed(ActionEvent e) {
    String text = thisTextField.getText();

    JTextField fieldFromFrame = frame.getTextField();
    fieldFromFrame.setText(text):
}

Also, you need to use Java naming convention. Class names being with capital letter. So First not first

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

If you "don't want open first frame again" then you must avoid creating a new instance of first and use a reference of your previous instance instead.

So you need a reference... one way to give your second a reference to first is provide it in the constructor, like:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         
  second sec = new second(this);  //notice I'm passing a reference to this as an argument second's contstructor   
  sec.jTextField1.setText(this.jTextField1.getText()); // this works
  sec.jTextField2.setText(this.jTextField2.getText()); 
  sec.setVisible(true);

Then you need to store the reference to first in your second class and use it to access textfields:

private first f;
public second(first referenceToFirst){
  this.f=referenceToFirst;
}

ok, then use f field instead of instantiating a new first when clicking the button on second...

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         
  //first f = new first();  //don't instantiate a new first, use the one you got in the constructor
  f.jTextField1.setText(this.jTextField3.getText());
  f.jTextField2.setText(this.jTextField4.getText());
}

Notes:

I wouldn't make textfields public static, I'd rather provide getter methods.

I suggest you adhere to naming conventions: use capital letters for classes and use significant names for variables.

Dario
  • 548
  • 1
  • 7
  • 14
0

try to passe your second jframe as parameter in the first jframe than use it to change your textfield