Send data to previous JFrame

允我心安 提交于 2020-01-17 12:38:00

问题


I am working with JFrames, I have a main JFrame, which calls for a second JFrame (jFrame2 for example). I need at that jFrame2 has 9 buttons with images within each button (UP HERE ALL PERFECT). Then when in the jFrame2 press a button, close the jFrame2 and send a string to jFrame1. the jFrame1 must be waiting for the end jFrame2 to receive the string that sends the jFrame2, and since the jFrame1 send a JSON to the server with that string.

This is my example code:

////// class jFrame1 //////

public class jFrame1 extends javax.swing.JFrame{

   public JTextField JTextField1; 
   public JTextField JTextField2; 
   public JTextField JTextField3; 
   private JButton jButton1;

   .....

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   jFrame2 regPack = new jFrame2();
   regPack.setLocationRelativeTo(null);
   regPack.setVisible(true);

   //HERE I WANT THE PROGRAM RESPONSE TO WAIT CLOSE AND CONTINUE RUNNING JFrame

   ....
}



////// class jFrame2 //////

public class jFrame2 extends javax.swing.JFrame{

   private JButton jButton1;
   private JButton jButton2;

   public jFrame2(){
   }

   .....

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   //HERE I WANT TO SEND A STRING ON JFRAME1 BEFORE CLOSING THIS JFRAME2
   this.dispose();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
   //HERE I WANT TO SEND A STRING ON JFRAME1 BEFORE CLOSING THIS JFRAME2
   this.dispose();
}

回答1:


Change it so that frame2 has a method (getter) that frame1 can retrieve the value that was set.

Change it so that frame2 is a modal JDialog. This will cause you code to stop at the point that the JDialog is made visible, but allow the rest of the UI to remain responsive. When the dialog is closed, the code (in frame1) will continue executing and you can retrieve the value from frame2 via the fore mentioned getter.

Take a look at How to make dialogs for more details...




回答2:


u may try like this:

public class jFrame1 extends javax.swing.JFrame{
  // ur code
  private String string;

  public void setString(String string) {
    this.string = string;
  }
}

public class jFrame2 extends javax.swing.JFrame{
  // ur code
  private jFrame1 frame1; // <-- note its jFrame1 not JFrame

  public jFrame2(jFrame1 frame1) { // <-- note its jFrame1 not JFrame
    this.frame1 = frame1;
  }

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    this.frame1.setString("wat u wana send here");
    this.dispose();
  }

  // so on
}



回答3:


Okay, so the solution is quite simple. Edit your classes as follows:

public class jframe1 extends JFrame{
    .
    .
    private String response;
    private void jButton1ActionPerformed(ActionEvent evt) {
        jFrame2 regPack = new jFrame2(**this**);  // pass the current reference of the frame
    .
    .
    .
    }

    public setResponse(String response) {
        this.response = response;
    }
    .
    .
    .
}

public class jframe2 extends JFrame {
    .
    .
    .
    .
    jframe1 jf1;
    jframe2() {
    }

    jframe2(jframe1 jf1) {
        this.jf1 = jf1;
    }


    private void jButton1ActionPerformed(ActionEvent evt) {
        jf1.setResponse("this is the response"); // set the response here
        this.dispose();
    }
    .
    .
}

Hope this helps.




回答4:


You can have a parameterised constructor for JFrame2 where it accepts an instance of JFrame1

public JFrame2(JFrame1 frame) {...}

JFrame2 can have a variable to store the value of JFrame1. When button is pressed, you can call method in JFrame1 as follows:

frame1.setString("...");

You can pass instance of JFrame1 to JFrame2

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   jFrame2 regPack = new jFrame2(this);
   regPack.setLocationRelativeTo(null);
   regPack.setVisible(true);

   //HERE I WANT THE PROGRAM RESPONSE TO WAIT CLOSE AND CONTINUE RUNNING JFrame

   ....
}


来源:https://stackoverflow.com/questions/23008090/send-data-to-previous-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!