This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JPanel not displaying in pop up window(jFrame) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel not displaying in pop up window(jFrame)" Watch "JPanel not displaying in pop up window(jFrame)" New topic

JPanel not displaying in pop up window(jFrame)

Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
From my desktop applications main window(jframe) I open a new window(jframe). I have gotten the contentpane for the new window and added a jpanel to it(which has a jLabel). The new window appears just fine but thats it.. the contents I have added(jLabels) do not appear.. just an empty gray area.. Do you have to do something different with JFrame's when they are a new window from you app's main window?
Joe Ess

Joined: Oct 29, 2001
Posts: 9126

Nope. I just posted an simple example of such a thing here.
Show us some code.

[How To Ask Questions On JavaRanch]
Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
Hi.. the code is below.. I've tried with and without setting a layout manager with no luck.. its probably something simple:

package AdMakerApp;
import java.awt.Container;
import javax.swing.*;
import java.awt.*;
public class OneMomentFrame extends JFrame{

Container contentPane;

public OneMomentFrame(){

setTitle("One Moment Please...");

setBounds(AdMakerApp.screenSize.width/2, AdMakerApp.screenSize.height/2, 300, 300);

contentPane = getContentPane();

contentPane.setLayout(new BorderLayout());
JPanel oneMomentPanel = new JPanel();
JLabel oneMomentLabel = new JLabel("One Moment Please...");
contentPane.add(oneMomentPanel, BorderLayout.NORTH);

**** Im calling the new window with OneMomentFrame oneMoment = new OneMomentFrame(); and then oneMoment.setVisible(true);. The window appears okay, my JLabel simply isn' there. Thanks in advance!
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Make a call to "pack();" as the last thing in your constructor. This gives the layout manager a change to arrange (size and place) the components it is managing.
Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
I tried using pack() but all that seems to happen is that you do see any of the window, just the title bar which makes me wonder if the JPanel is not getting added to the jframe somehow.
Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
I meant "dont see any of the window" above..
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
JFrame's default Layout is BorderLayout.
If you want the BorderLayout to remain, do not call contentPane.setLayout(new BorderLayout());
just add the component like:

also, try moving the setBounds call to the end.

An egg is a chicken's house!
I agree. Here's the link:
subject: JPanel not displaying in pop up window(jFrame)
It's not a secret anymore!