0

I am from system programming background in C and Java programming is Greek and Latin for me.

So my problem is:- I have 2 JFrame Objects

  • I have a parent Object A
  • which has child Objects B.

Object B has buttons on it. If a button is clicked in B I want to update the UI of parent Object.

How can I can communicate to the parent - to update itself?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
avimonk
  • 173
  • 2
  • 11

3 Answers3

3

Do the updates need to be made 'live' while the values are adjusting and the 2nd GUI is open, or can they be delayed until it is closed?

They can be delayed until it is closed.

Use a modal dialog or JOptionPane instead.

Using a modal dialog, whatever code line comes immediately after setting it visible, is blocked from being processed until it is closed. That is where you examine (the return value of the option pane &) the value of the controls you put in the dialog. Note that the idea here is do not extend dialog. Instead just create an instance of one in the main code, create the controls, add them to it, and show it using the main frame as the parent.

Putting 'everything in one class' is not a good design, nor what I am suggesting in general. It is just that there is little cause here for the main GUI not to have references to controls that affect/update it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Well normally you have a parent JFrame object into which you put your widgets like buttons, panels etc. You can simply add an anonymous action listener with the button and then call any function of the parent class from inside it.

public class MyFrame extends JFrame{
  private Button button 2 = new Button();

  public void init(){
       b = new Button("Click me");
       b.addActionListener(new ActionListener(){
                                       public void actionPerformed(ActionEvent e) {
                                        //CALL ANY FUNCTION OF PARENT CLASS FROM HERE.
                                        updateText();
                                       }
                                      });
  }

    updateText(){
    this.button2.setText("new text");
  }
}

You can find more info about it on this page http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Apurv
  • 4,458
  • 2
  • 21
  • 31
  • I have implemented the Listener. However my question is bit different. I want to call the parent Object from my action Listener. Thanks neways, – avimonk Jun 13 '12 at 20:31
  • You can call any function of the parent class which is a JFrame class from inside the actionPerformed() method. – Apurv Jun 13 '12 at 20:32
  • 1
    @Apurv Sure, but you are missing the bigger picture. The OP is launching a new GUI from within that button - it has its **own** controls & they have no direct reference back to the main GUI, nor does it have a reference to those controls. – Andrew Thompson Jun 13 '12 at 20:36
  • only if the addEventListener() is inside the parent class. – 11684 Jun 13 '12 at 20:36
  • Thats the problem I donot know how to access the parent class. Child is not aware of the parent. – avimonk Jun 13 '12 at 20:38
0

I have found one more way to solve this problem.

I guess it can solve a general delegation problem.

Parent Object composes of Child Object. Parent Object passes this value to Child Object in constructor. This way the Child can call a method on the Parent Object and notify any change.

In this case Child Frame calls Parent frame and asks the parent to refresh itself.

avimonk
  • 173
  • 2
  • 11