How to let two windows (JPanel or JFrame from two different classes) communicate with each other?

Dear All:

In my program, I had a JPanel(or inside JFrame) object when running the program, which has a JButton inside it, when I click on the Jbutton, it pop up (create) another JPanel object (not the same class as the original one) which will display the information passed from the original JPanel (e.g. a text string) in this new JPanel. Now When I click on the JButton on this new JPanel, another text string needs to be passed to the original JPanel and displayed on it. Then the newly pop-up JPanel will disappear.

My question is how to easily keep the communication between these two JPanels (two-way communication as described above)? Could you please provide a piece of codes just show how to do it?

Thanks a lot!

yi02


yi02Asked:
Who is Participating?
 
objectsCommented:
You need to use the first panel as the action listener for the button in the second panel

So the constructor in 2nd panel would have something like:

public class SecondPanel(ActionListener first)
{
   ...

   JButton button = new JButton("Push Me");
   button.addActionListener(first);
   ...
}

And when you create your second panel the code would look something like:

ActionListener listener = new ActionListener()
{
   public void actionPerformed(ActionEvent event)
   {
      buttonPressedInSecondPanel();
   }
};

panel2 = new SecondPanel(listener);


That way the method buttonPressedInSecondPanel() will get called in the *first* panel, when the button is pressed in the *second* panel.

Let me know if you have any questions :)
0
 
aozarovCommented:
extend JPanel and add a method to set the other panel and another method to return the needed text.
Then connect those two panels using the first method and use their values returned from the second method.
0
 
yi02Author Commented:
Dear objects:

Thanks a lot for your comments. I was away for two weeks and sorry about the late response.

Your code appears covering most of my need but a side point:
I need pass a text string from the second panel to the first one, how to do that?

Thanks again,

yi02



0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
aozarovCommented:
You can pass it in the constructor
e.g.
panel2 = new SecondPanel(listener, panel1.getTheNeededString());
or have a method in panel2 (or panel1) that accept the string.

panel2.setPanel1String(panel1.getTheNeededString());

Of course you will need to implment setPanel1String and getTheNeededString.
Another option is to pass one panel to the other panel in its constructor and the call otherPanel.getTheNeededString() when you need to (as I suggested above).
0
 
yi02Author Commented:
Dear aozarov:

Thanks for your comments. But my question is How to pass a text string from the SECOND panel to the FIRST panel during running the program, not at the time initialize the second panel. In other word, after creation of the second panel object from the first panel, the user do other things on the second panel, and then user click a button in second panel to pass a text string to the first panel and also terminate the second panel. Could you please put more detailed codes there so that I can follow what you suggested. Thanks a lot.

yi02
0
 
aozarovCommented:
Assuming you have a subclass of a Panel named SecondPanel.
Change the constructor of that pannel so it will accept the first panel as an argument
in addition to the ActionListener.
e.g

panel2 = new SecondPanel(listener, panel1);

add a method in panel one that returns the value you want.

then in panel2 call panel.getValue() [the method you created in panel1] when you need it.
0
 
yi02Author Commented:
Dear aozarov:

Thanks for your input and good idea. But  when I tried to implement it, I run into a dilemma: as I said before, I need pass a string from panel 2 to panel 1, however, I want to set the string value in panel 2 before send it back to panel 1 and use the string to do something in panel 1. So in a button in panel 2, I set two actionlisteners for it (panel 2 itself and the actionlistener from panel 1 (ActionListener first) that has been passed to the constructor of panel 2 by panel 1.  
Now in the actionPerformed method of panel 2, I tried to set the string value that needed to be passed to panel 1. So when I click the button, it supposed to set the string value before send it back to Panel 1. However, the actionlistener from panel 1 (ActionListener first) also respond to the button-click in panel 2 at the same time, which try to call a method in panel 1 to use the string which has not been set value yet. My question is: is there anyway to let the two listeners act in a suquential manner: e.g. the actionListener for panel 2 respond first (after click the button) to set the string value in panel 1, then the actionlistener from panel 1 (ActionListener first) respond and call a method in panel 1 to use the string value to do something. My question is whether there is a way to let two action listeners of a single object respond sequentially?

By the way, I get arround this problem by use button.setActionCommand in panel 2 and getActionCommand of the actionevent in panel 1 (passed from panel 2 to) pass the string. Just curious about the above question I just asked?

Thanks  a lot.

yi02
0
 
objectsCommented:
> My question is: is there anyway to let the two listeners act in a suquential manner:

Technically no, but in practice they (the listeners) are called in the order they are added.
0
 
aozarovCommented:
> My question is: is there anyway to let the two listeners act in a suquential manner:
You can override the fireActionPerformed to do what first and then delegate the call to the listeners (by calling super...)
BTW, the invocation appears to by done in reverse to the insert order, hence "The listener list is processed in last to first ".
Use this example to experiment:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestButton
{
      public static void main(String st[])
      {
            JFrame frame = new JFrame("test");
            JButton test = new JButton("test")
            {
                  protected void fireActionPerformed(ActionEvent e)
                  {
                        System.out.println("fireActionPerformed");
                        super.fireActionPerformed(e);
                  }
            };

            test.addActionListener(new ActionListener()
                  {
                        public void actionPerformed(ActionEvent ev)
                        {
                              System.out.println("Added first");
                        }
                  });

            test.addActionListener(new ActionListener()
                  {
                        public void actionPerformed(ActionEvent ev)
                        {
                              System.out.println("Added second");
                        }
                  });

            frame.add(test);
            frame.pack();
            frame.show();
      }
}
0
 
objectsCommented:
Yes action listeners are currently fired last to first (but there is no guarantee that a different version will use the same order). Your code should not rely on the order listeners are notified.
If a listener needs to be notified after another listener has been notified then it would sound like you would be better off having the depending listener notify the other listener itself.
0
 
yi02Author Commented:
Dear aozarav and objects:

Thanks a lot for yor guys' wonderful input. Appreciated a lot!!!

Best regards,

Ming
0
 
yi02Author Commented:
Dear aozarav :

I intended to split the points to both of you. But I accidently did the wrong way by only accept one answer from objects. Is there any way to reverse it? Or I just post the same question again, you just put some comments there, then I accept it as answer, how is that? Sorry about this.

yi02
0
 
zzynxSoftware engineerCommented:
>> Is there any way to reverse it?
Yes. Ask to reopen this question.
This is how:
Post a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21387931.html
0
 
aozarovCommented:
Hi yi02,
I appreciate the fact that you value my efforts :-)
As that is more important for me then the points I am fine as is :-)
Take care,
Arie.
0
 
yi02Author Commented:
Dear Arie:

Thank you so much. I still will try to restore your credit if I can.

best wishes,

yi02
0
 
yi02Author Commented:
Thanks for your efforts, OzzMod

yi02
0
 
aozarovCommented:
yi02, Thanks :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.