• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null layout JPanel displays on JFrame but not regular JPanel

 
Kevin Ryan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thePanelInQuestion.setPreferredSize(new Dimension(100,100));
(where 100 is any size you want)
 
Kevin Ryan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic