-1

In java, Say I have a JFrame (say JFrame2) which is created by clicking a button on the first Jframe (JFrame1). How do I get a selected value from the second (Jframe2) back to the first one without creating a new instance of the first frame (Jframe1).

i.e I want to make Jframe2 a subform of JFrame1. Just the way a JOptionFrame.showMessageDialog() would return a value.

Eric Leibenguth
  • 4,167
  • 3
  • 24
  • 51
Victor Ikenna
  • 45
  • 1
  • 8
  • Why is this tagged `javascript`? – Lukas Rotter Sep 16 '15 at 17:38
  • 2
    Just incase a javascript programmer has the knowledge – Victor Ikenna Sep 16 '15 at 17:40
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Sep 17 '15 at 01:45
  • *"Just incase a javascript programmer has the knowledge"* That could be an excuse for slapping any tag you like, on a post. That is not what tags are for. Tags are to identify the things that are *actually relevant* to the problem. If you want the solution in Java, then it has nothing to do with JavaScript. – Andrew Thompson Sep 17 '15 at 02:01

1 Answers1

0

I can't test my code here, so maybe you need to fix some points.

Basically you can pass an instance from frame1 to frame2.

class CustomFrame extends JFrame {

    private CustomFrame parent;

    public int someField;

    public CustomFrame(CustomFrame parent) {
        this.parent = parent;
    }   
    private void someLogic() {      
        // some logic       

        //udpate parent;
        parent.someField = 10;
    }

    public static void main(String args[]) {        
        CustomFrame parent = new CustomFrame(null);
        CustomFrame child = new CustomFrame(parent);        
        child.someLogic();      
    }   
}

If you need to execute some code after child close, I suggest you to use JDialog with setModal(true)

Johnny Willer
  • 3,717
  • 3
  • 27
  • 51
  • *"If you need to execute some code after child close, I suggest you to use `JDialog` with `setModal(true)`"* That information should be **first** in the answer, as it is the correct way to approach this problem. If the programmer creates a field (e.g. `JTextField`) for the dialog (both of which are created in the code of the main frame) then the code can simply show the dialog, then get the value of the field in the next code line. Easy! – Andrew Thompson Sep 17 '15 at 01:59