Forums Register Login

Communicate between JFrames

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Trying again:

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1559 times.
Similar Threads
updating JPanel in a JFrame
removing a painted component via paintComponent leaves a residue
JFileChooser: Selecting multiple files error
How should child GUI components access their parents in MVC?
How to make default JButton in child class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:38:59.