0

I have this code:

my main Frame:

   public MainFrame() {
    initComponents();
  }

My other frame is initialized like this in my main frame:

     private void agregarAlumnoActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    new AgregarAlumno().setVisible(true);

}  

which opens the second frame:

  public AgregarAlumno() {
    initComponents();
}

I then will show a text box in the second frame to input something and i would like it for it to be used by my Main Frame, i know i can send a value to the second frame like this

       new AgregarAlumno(valueX).setVisible(true);

and then on the Second Frame receive it building a constructer like this

    public AgregarAlumno(int valueX) {
    initComponents();
}

and use it in the second Frame. but how would i send back a value to the first Frame?

Thanks.

  • 2
    If all you want is a value from a TextField, maybe [`JOptionPane.showInputDialog`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html#showInputDialog-java.awt.Component-java.lang.Object-) might be a better solution – René Blanken Dec 04 '15 at 06:48
  • 1
    yes i think i will use this instead, thanks. – Erik Macias Dec 04 '15 at 06:54
  • If You can keep a static reference to the text field on the frame 1 you can send the value to the text field from the second frame. However, @RenéBlanken solution should be considered if you just need a value from one/. – Vivek Singh Dec 04 '15 at 06:54
  • well it is more than one value, how would i send it back to that static reference? just = ? – Erik Macias Dec 04 '15 at 07:02
  • found the answer to that static reference – Erik Macias Dec 04 '15 at 07:06
  • *"it is more than one value"* For that I'd put all the controls in a single panel and show that panel in a `JOptionPane.showConfirmDialog(..)`. Check the return result and if the user OK'd it, get the values from the controls (or their models) and use it, otherwise ignore them and continue. But definitley +10 to @RenéBlanken for the tip - a modal dialog is the best approach here. See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Dec 04 '15 at 07:29
  • [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer Dec 04 '15 at 08:25
  • @ErikMacias No, `static` IS NOT the answer, it is how you dig yourself into a very deep hole and marvel at when your program unravels itself – MadProgrammer Dec 04 '15 at 08:26

1 Answers1

0

// you can passvalue from one text field to other like this // this is just simple example

public MainFrame() {
    public void actionPerformed(ActionEvent ev){
         FrameOne frameOne = new FrameOne(userField.getText(),passField.getText());
         frameOne.setVisible(true);
    } 
} 


public class FrameOne extends JFrame
{
    private String user;

    private String pass;

    public FrameOne(String usr, String pas){

        this.user=usr;

        this.pass=pas;

       //components

   }
}
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49
Sagar510
  • 16
  • 2