-2

I have a jFrame1, two JTextField. One of the text fields should load the data from my jFrame2. In my jFrame1 I have a button that opens the jFrame2. When you press the button opens jFrame2, you can see 4 buttons, and when you press one of the buttons, the jframe2 should close and load a string in one of my text fields.

Anyone know how I can do this? Because I have tried several codes and does not leave me.

This is my example code:

public class jFrame1 extends javax.swing.JFrame{

   public JTextField txt1; 
   private JButton btn1;

   btn1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
         jFrame2 jframe2 = new jFrame2(this);
         jframe2.setVisible(true);
      }
   });


   .....

}


public class jFrame2 extends javax.swing.JFrame{

   private JFrame jframe1;

   public jFrame2(JFrame jframe){
      this.jframe1 = jframe;
   }

   ...
   jframe1.txt1.setText("Hallo!");
   this.dispose();
   .....

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Despotars
  • 541
  • 1
  • 8
  • 23
  • 2
    What have you tried ? shows what you tried then we will help you :) ! No code = nothing happened <- this is golder principle of StackOverflow ! – Maciej Cygan Apr 10 '14 at 11:33
  • Write a getter or just let the String be public and use the References of your JFrame to access the getter or String directly. – Leo Pflug Apr 10 '14 at 11:36
  • Yes! sorry, here you can read my example code – Despotars Apr 10 '14 at 11:47
  • 3
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) This sounds like the case for a modal `JDialog` or a `JOptionPane`. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 10 '14 at 11:53
  • Thank you Andrew! then, do you think would be better make use of jDialog or jOptionPane? – Despotars Apr 10 '14 at 11:57
  • 1
    I recommend JOptionPane for this. Dialog is for more complex interaction. If you just want one text, JOptionPane is the way to go. – Angelo Fuchs Apr 10 '14 at 11:58
  • Tip: Add @AngeloNeuschitzer (or whoever, the `@` is important) to *notify* the person of a new comment. And I agree, `JOptionPane.showInputDialog(..)` might save a lot of time. – Andrew Thompson Apr 10 '14 at 12:29
  • @AndrewThompson The owner of the Stack (in this case the OP of the Q) gets the messages anyway. And I didn't wanted to include you :) But yeah, a useful feature. – Angelo Fuchs Apr 10 '14 at 12:51
  • @AngeloNeuschitzer The 'tip' was to the OP, rather than you. I wasn't talking to you, just about you. ;) It seemed simpler given I was about to agree with you. – Andrew Thompson Apr 10 '14 at 13:01

1 Answers1

0

There are too many ways,

One of those is provide a constructor to your frame with String parameter and pass the value.

For example,

public class jFrame1 extends javax.swing.JFrame{

   public JTextField txt1; // Hope you have initialized this somewhere in your code else you will face a NPE.
   private JButton btn1;

   btn1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
         jFrame2 jframe2 = new jFrame2(this, txt1.getText());
         jframe2.setVisible(true);
      }
   });


   .....

}


public class jFrame2 extends javax.swing.JFrame{

   private JFrame jframe1;
   private String text;
   public JTextField txtDemo; 

   public jFrame2(JFrame jframe){
      this.jframe1 = jframe;
   }

   public jFrame2(JFrame jframe, String text){
      this.jframe1 = jframe;
      txtDemo = new JTextField(text);
   }

   ...

}

You can find answer in this SO question also.

And refer the answer on The Use of Multiple JFrames, Good/Bad Practice? before implementing this, a good explanation on JFrame and Swing is given.

Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80
  • Thank you for you response. One question, in the last of the code, in this sentence: "jframe1.txt1.setText(text);" The compiler tell me this: "cannot find symbol symbol: variable txt1 location: variable jframe1 of type JFrame" – Despotars Apr 10 '14 at 12:05
  • 1
    You will get a null pointer also if you will run because `txt1` in jframe1 is not initialized, and here is an demo of sending text of `txt1` which is in `jframe1` to `jframe2` and this text will be displayed in `txtDemo` of `jframe2`....and please start with basic tutorial of java swing. – Not a bug Apr 10 '14 at 12:10
  • This is coding hacks to account for a deficiency in design that can be easily solved using a modal dialog. – Andrew Thompson Apr 13 '14 at 06:36