File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes null layout JPanel displays on JFrame but not regular JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "null layout JPanel displays on JFrame but not regular JPanel" Watch "null layout JPanel displays on JFrame but not regular JPanel" New topic
Author

null layout JPanel displays on JFrame but not regular JPanel

Kevin Ryan
Greenhorn

Joined: Jun 23, 2006
Posts: 5
I have a small JPanel using a null layout manager. When I add it directly to a JFrame, it is visible. When I add it to another null layout JPanel it is visible. When I add it to a JPanel with the default layout it does not show up.

Does anybody know the reason why?

Thank you,

Kevin Ryan
National Radio Astronomy Observatory
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
thePanelInQuestion.setPreferredSize(new Dimension(100,100));
(where 100 is any size you want)
Kevin Ryan
Greenhorn

Joined: Jun 23, 2006
Posts: 5
Boy, don't I feel stupid!

Thanks for the quick and helpful response Michael.

I tried 'setSize' and 'setBounds' but not 'setPreferredSize'

Can I bother you to tell me the difference between setSize and setPreferredSize and why one works and not the other in this situation? The api has the usual "sets the preferred size ..."

Kev
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
setBounds(), or setSize() with setLocation() are required when using a null layout

layout managers use setPreferredSize(), so if your panel has no preferred size
(perhaps it has no components, at this stage), it will default to (0,0) and be 'invisible'

if the panel/container uses a layout manager, using setSize(..) for its added
components will not work
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null layout JPanel displays on JFrame but not regular JPanel