• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Communicate between JFrames

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying again:

 
Ricky Clarkson
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic