This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes sharing Panel between frames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "sharing Panel between frames" Watch "sharing Panel between frames" New topic

sharing Panel between frames

amarnath srinivasan

Joined: Mar 28, 2005
Posts: 2
I would like to share a panel between two frames. How do I do that?

In detail, I have a panel, that has the controls(move control and position info) for a motor. In main frame, I display only the position information from the panel and make the other controls invisible. When a user wants to move motor, he clicks a button on this frame. On the button control, I create a new frame and pass the panel to the new frame and make all controls(move and position info) visible. Then the user can move the motor and when he is done can clos the new frame to revert back to the main frame. The problem is when I revert back to the main frame the panel is not there any more even if I try to repaint. How do I solve this problem?

I also have one more question. For the above problem is it okey to use multiple frames? what do you guys think. One other idea is to use a single frame and change the content pane of ythe frame with the motor controls when I click a button. But the problem here is the code will be cluttered with this controls for multiple panels.


amarnath srinivasan
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The rule is that you can add a component to only one container. If you add a component to one container and then add it to another, it is removed from the first and added to the second. You could pass the component around from one JFrame to another but this seems inelegant. For options you could launch a JDialog to show the controls and to get user input. You could add a control panel to your JFrame and use the button to toggle it visible/invisible. Swing offers the JTabbedPane that can be useful here. Another option is the AWT CardLayout used here:
I agree. Here's the link:
subject: sharing Panel between frames
It's not a secret anymore!