Link to home
Start Free TrialLog in
Avatar of yi02
yi02

asked on

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


Avatar of aozarov
aozarov

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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yi02

ASKER

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



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yi02

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yi02

ASKER

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
> 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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of yi02

ASKER

Dear aozarav and objects:

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

Best regards,

Ming
Avatar of yi02

ASKER

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
>> Is there any way to reverse it?
Yes. Ask to reopen this question.
This is how:
Post a zero-point question in https://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
https://www.experts-exchange.com/questions/21387931/How-to-let-two-windows-JPanel-or-JFrame-from-two-different-classes-communicate-with-each-other.html
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.
Avatar of yi02

ASKER

Dear Arie:

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

best wishes,

yi02
Avatar of yi02

ASKER

Thanks for your efforts, OzzMod

yi02
yi02, Thanks :-)