aspose file tools
The moose likes Swing / AWT / SWT and the fly likes JFrame in a JFrame? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame in a JFrame?" Watch "JFrame in a JFrame?" New topic

JFrame in a JFrame?

Art Vandalay

Joined: Nov 09, 2009
Posts: 3
Is it possible for a JFrame to contain another JFrame? This is a question posed by the the following post from 2008.

I have created a simple stand alone JFrame-based graphical application that needs to be the background for various other frames/dialogs. I tested my stand alone with JInternalFrames, and it worked just the way I wanted. However, when I went to integrate with a larger app (which is it's intended purpose), I discovered that app was JFrame-based as well. Looking through that code, it seems very problematic to try and change it to JInternalFrame. In addition, the main project also launches various JDialog based windows as part of it's normal operation.

The entire purpose of my app when bundled with the main app is to act as a background or container for that app. Specifically, I have two requirements:

1) My JFrame needs to always be on the bottom, behind all other frames/dialogs.
2) My JFrame needs to be the bounding area of all the other frames/dialogs.

Working with JInternalFrames, this comes naturally. But as it stands now, the best I can do is instantiate my app, then the main app as well. And that main app is in no way bounded to or by my JFrame. Does anybody know a way around this? I'm trying to alter the main apps code as littel as possible and do all the work or fancy stuff in my code. I would appreciate any advice that anybody has to send my way. Here is an tiny example of what I'm trying to do, that is failing.

Thanks again, and any alternative approaches are, of course, welcome.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
If you haven't already, have a look at The JInternalFrame class, JInternalFrame can exist inside of a JFrame.

edit, OK I sshould have looked closer at your post first, I see you know about JInternalFrame already . Sorry
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2243
A frame is represented by the components added to it. So you do something like:

somePanel.add( frame.getContentPane() );

or maybe

somePanel.add( frame.getRootPane() );
Art Vandalay

Joined: Nov 09, 2009
Posts: 3
Rob and Fred, thanks for the timely responses. Rob, I tried your idea. It does part of the job. Basically, I see it takes the contents of JFrame2 and dumps them statically into JFrame1. Perhaps... no I'm certain... I did not explain the problem properly. So to add to my previous problem statement and hopefully succinctly summarize, I'm basically looking to have a JFrame, and various JDialogs owned, or contained within a "master" JFrame, with the same type of relationship that a JInternalFrame has to a JFrame. In other words, I wish to have the JFrame and JDialogs remain looking like a Dialog, with all the associated title bars, etc. However, they will be constrained (location wise) by the "parent" JFrame. Really, if I could figure out a way to restrict their movement so they couldn't be dragged outside the area of the master JFrame (which would be always placed in back), that would be an adequate solution. Because that gives the impression that the master JFrame is the "container" whether it really is or not. Hope I'm doing a decent job describing the issue.


Art Vandalay

Joined: Nov 09, 2009
Posts: 3
I was able to hack away at the existing code very minimally, and change the JFrame to JInternalFrame. Now it behaves mostly as I'd like. However it does create several JDialogs and are not playing nicely (they're not constrained to within the JFrame). Any ideas?
joe shmo

Joined: Dec 04, 2009
Posts: 1
i ran into the same problem making a game of battleship for a final project in class, and decided near the end that it would look cool to add pictures of the ships where they were placed, rather than just gray squares. needless to say after a few thousand lines of code without ever thinking about adding images, it was difficult to shoe-horn them in. and once i did they would disappear behind the main game board's frame when you clicked it to add another ship.

i found a great answer while searching google.


that one line of code did exactly what i wanted, making my main game board always in the background no matter what i added. very easy, very quick.

also, sorry for bumping an old post
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: JFrame in a JFrame?