aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Communicate between JFrames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Communicate between JFrames" Watch "Communicate between JFrames" New topic
Author

Communicate between JFrames

Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
Hi,

I have a couple of questions about JPanel->JFrame communication:

1. I am opening a JFrame inside an ActionListener on a JButton inside a JPanel. Can I communicate with the JPanel from the JFrame via getParent() or some other method?

2. I am opening serveral JPanels inside a JFrame. Can I communicate with this JFrame via getParent() or other method?

3. In a situation with a JFrame with a primary JPanel (JFrame->JPanel), when I call JPanel.getParent(), I receive another JPanel, and it looks like a reference to the calling JPanel. Why don't I receive a reference to the JFrame? Is there a way to obtain this reference?

4. Is there a better way to open a new window than as described in #1? I basically have a main window (JFrame->>JPanel), that needs to allow the opening of another complex window to edit certain properties (JFrame->>JPanel). Given the way I am doing it (opening new JFrame from ActionLister) it looks like I will have to pass a reference to the original JPanel that contains the open button.

...Mike
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
You can keep references to whatever you like. Don't rely on getParent(), because it often will surprise you. The parent of a button on a JFrame won't be the JFrame, but the content pane, for example.

Post some actual code where you'd like to have access to a certain variable, but you currently don't, i.e., some code that fails to compile, for just this one reason.
Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
I was looking for a way to avoid the messy references.

Maybe I am looking more for a design pattern. Here is what I am looking at (I hope the formatting works):

/- JTable
/- EditTablePanel<
EditFrame - EditPanel<
\ /- other buttons
\- EditButtonPanel<
\- AddButton -opens-> AddFrame

/- input fields
AddFrame - AddPanel<
\- SaveButton -send input values to EditTablePanel


It seems like the tree gets deep very fast. Plus, once I open AddFrame, getParent seems to be cut off from the rest of the app.

Is there a neat way to return the values from the AddPanel input fields to the JTable on the EditTablePanel?

...Mike
Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
Trying again:

Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
My guess is that you are subclassing everything, so instead of writing code that sets up a JFrame, etc., you're writing code that extends a JFrame.

If you are doing the former, I'd suggest not doing, but anyway - EditFrame needs a reference to EditPanel.

EditPanel needs a reference to EditTablePanel and EditButtonPanel:

EditTablePanel needs a reference to a JTable - you get the idea.

SaveButton needs a reference to EditTablePanel, so give it one the same way as above.

If that turns out to be unwieldy, take a look at the observer design pattern.
Mike Broadbear
Ranch Hand

Joined: Jan 14, 2002
Posts: 39
Yes, I am subclassing each of the frames/panels from JFrame and JPanel. I came to it after trying a couple of other methods (wrapping, etc...), none of which fit quite right.

What do you suggest other than subclassing? Can you point to some literature/concepts describing best practices?

...Mike
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Here's some literature on it: http://www.swingwiki.org/best:using_a_factory

If that's not enough, say so and I'll provide a link to the lecture notes I used this semester - currently all the notes are externally visible, but the index is only available for our students.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Communicate between JFrames