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 Multiple JFrames in 1 Container 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 "Multiple JFrames in 1 Container" Watch "Multiple JFrames in 1 Container" New topic

Multiple JFrames in 1 Container

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 751
Hi, is this advisable? I was trying to create multiple windows with JFrame with only one instance of the Container class. I do it like this...

JFrame window1 = new JFrame("Window 1");
JFrame window2 = new JFrame("Window 2");

Container container = window1.getContentPane();
container.setLayout(new FlowLayout());

window1.setSize(200, 100);

window2.setSize(200, 100);
window2.setVisible(200, 100);

I've tried this and it worked. I didn't paste the whole code because I'm typing on my mac and surfing the internet through PC. I'd just like to ask if it's ok to do this practice? Or is ther something I should avoid in doing this? Thanks!

SCJP 1.5
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Hmmm... I'd like to see what happens when you try to add components to the second JFrame.

Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
So, I tried it.

Compiles just fine, but running it....

Exception in thread "main" java.lang.Error: Do not use javax.swing.JFrame.add()
use javax.swing.JFrame.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(
at javax.swing.JFrame.addImpl(
at java.awt.Container.add(
at Testing.<init>(
at Testing.main(

So I'm thinking you can't use your container for both windows without some fancy foot work.

And this example won't compile.

Compile error: container is already defined in Testing()

BUT! You can do this.

Thanks! That was fun!
[ October 12, 2005: Message edited by: Stephen Boston ]
cat cater
Ranch Hand

Joined: Jun 06, 2005
Posts: 39
Multiple JFrames in 1 Container ? why don't use JDesktop and JInternalFrame?
JPanel pt=new JPanel(new BorderLayout());
JDesktop desk = new JDesktop();
JInternalFrame inner1 = new JInternalFrame();
JInternalFrame inner2 = new JInternalFrame();
JInternalFrame inner3 = new JInternalFrame();
I agree. Here's the link:
subject: Multiple JFrames in 1 Container
It's not a secret anymore!