-1

Sorry, I have just review my little project and realize it is not I want to transfer data between JFrames.

It is like

Public Class JFrame2 Extends javax.swing.JFrame
{
    public void tryingToDoSomething(int i)
    {
        /*Do something to call setSomething().*/
    }

}

Public Class JFrame1 Extends javax.swing.JFrame
{
    private int something;
    public int setSomething(int something)
    {
        this.something = something;
    }
    private JFrame2 jFrame2 = new JFrame2();
    public void runThings()
    {
        jFrame2.setVisible(true);
    }
}

I have some content in the first JFrame, I call the second JFrame and with the second JFrame, I want to change the content of the first JFrame (like in my example, JFrame1 have "int something" and method "setSomething()", I called JFrame2 and use its method "tryingToDoSomething(int i)" to set the value of "int something" of JFrame1).

Still thank you very much for your answers.

  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jul 08 '15 at 07:57
  • Your second will require a reference to the instance of the first class. This is a relatively common question, perhaps you should see if you can any examples which might solve your problem first – MadProgrammer Jul 08 '15 at 08:04
  • Thank you very much for your answer, but I realized it is not I want to ask. I edited my question. Hope you can help me again. – Thành Nguyễn Jul 08 '15 at 08:31
  • 1) Tip: Add @MadProgrammer (or whoever, the `@` is important) to notify the person of a new comment. 2) It is likely that *neither* class should extend anything, let alone a `JFrame`. Simply keep a reference to an instance of each top level window and then pass them to whatever methods need to act directly on that instance. 3) If you were replying to me, note that I am not going to try to provide help for making this work with two frames. That's a problem that should be fixed *first.* – Andrew Thompson Jul 08 '15 at 08:35
  • Use a modal dialog to gather the information and when it's closed (returning to the caller), update the caller's state. [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer Jul 08 '15 at 08:37
  • And I almost forgot: 4) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). I.E. not uncompilable pseudocode, but compilable, runnable code. And please use more meaningful names than `JFrame1` or `doSomething()` - `LoginFrame` and `checkLoginDetails()` are things that other people can understand, and thereby better explain the problem domain. – Andrew Thompson Jul 08 '15 at 08:40

2 Answers2

0

You should try to do something like this:

public Class JFrame1  extends javax.swing.JFrame{
  private int something;
  public int getSomething()
  {

    return something;
  }
  private JFrame2 jFrame2 = new JFrame2();
  public void runThings()
  {
    jFrame2.tryingToDoSomething();
    jFrame2.setVisible(true);
  }
} 

Then

public Class JFrame2 extends JFrame1 {
 public void tryingToDoSomething()    {
   /*Do something to call getSomething().*/
   getSomething();
 }
}
Pankaj Dubey
  • 146
  • 10
0

How can you call the first JFrame's method by the second JFrame's method?

You need to have same reference in your second frame. You may try the following:

public class JFrame2 extends javax.swing.JFrame
{
    private JFrame1 frame1;// reference
    public JFrame2(JFrame1 frame1) {
       this.frame1 = frame1;// save reference
    }
    public void tryingToDoSomething(int i)
    {
        frame1.setSomething(i);//do something here
    }

}

public class JFrame1 extends javax.swing.JFrame
{
    private int something;
    public int getSomething()
    {
        return something;
    }
    public int setSomething(int something)
    {
       this.something = something;
    }
    private JFrame2 jFrame2 = new JFrame2(this);// note `this`
    public void runThings()
    {
        jFrame2.setVisible(true);
    }
}