?
Solved

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

Posted on 2005-04-13
19
Medium Priority
?
943 Views
Last Modified: 2013-11-23
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


0
Comment
Question by:yi02
  • 7
  • 6
  • 3
  • +1
17 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13777139
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
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13777214
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
 

Author Comment

by:yi02
ID: 13917280
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 1000 total points
ID: 13919062
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
 

Author Comment

by:yi02
ID: 13921592
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
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 1000 total points
ID: 13922064
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
 

Author Comment

by:yi02
ID: 13947659
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
 
LVL 92

Expert Comment

by:objects
ID: 13948565
> 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
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 1000 total points
ID: 13949279
> 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
 
LVL 92

Expert Comment

by:objects
ID: 13949335
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
 

Author Comment

by:yi02
ID: 13968015
Dear aozarav and objects:

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

Best regards,

Ming
0
 

Author Comment

by:yi02
ID: 13968112
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13968171
>> 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
 
LVL 15

Expert Comment

by:aozarov
ID: 13969984
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
 

Author Comment

by:yi02
ID: 13972386
Dear Arie:

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

best wishes,

yi02
0
 

Author Comment

by:yi02
ID: 14004546
Thanks for your efforts, OzzMod

yi02
0
 
LVL 15

Expert Comment

by:aozarov
ID: 14006014
yi02, Thanks :-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question