Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel not displaying in pop up window(jFrame)

 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
Thanks,
Matt
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. I just posted an simple example of such a thing here.
Show us some code.
 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...");
oneMomentPanel.add(oneMomentLabel);
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Thanks.
 
Matt Wilcko
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant "dont see any of the window" above..
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic