wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Multiple JFrames in 1 Container" Watch "Multiple JFrames in 1 Container" New topic
Author

Multiple JFrames in 1 Container

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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);
window1.setVisible(true);

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
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
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(JFrame.java:465)
at javax.swing.JFrame.addImpl(JFrame.java:491)
at java.awt.Container.add(Container.java:307)
at Testing.<init>(Testing.java:16)
at Testing.main(Testing.java:27)

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?
code:
-------------
JPanel pt=new JPanel(new BorderLayout());
JDesktop desk = new JDesktop();
JInternalFrame inner1 = new JInternalFrame();
JInternalFrame inner2 = new JInternalFrame();
JInternalFrame inner3 = new JInternalFrame();
pt.add(desk);
desk.add(inner1);
desk.add(inner2);
desk.add(inner3);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple JFrames in 1 Container
 
Similar Threads
Click to open another window, what is the problem with my ActionListen?
how to control components of another window
GridBagLayout question
How to pass an image of a JLabel to a JTextPane?
When calling another class, leaving the current class