Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom JComponent problem  RSS feed

 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've built my own custom component, overriding the paintComponent() method, and using the Graphics2D object. I have no problem displaying the component in a JFrame using getContentPane().add(component)...The problem I'm having is when I add the custom component to a JPanel then add the panel to the contentPane, I doesn't show up, it's either the size of a dot, or something. ny ideas?
Thanks
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably a problem with the component's size not being set correctly... JFrame's contentPane has a default BorderLayout, so doing an add() adds the component to the center and tells it to take up as much area as possible. JPanel has a default FlowLayout, so each component is displayed at whatever size is preferred by that component.

The easiest way to fix this is to add the method -

to your class, and return a Dimension that allows for the entire content of your component to be made visible.

getMaximumSize() and getMinimumSize() are also available, but are not used by layout managers to the extent that getPreferredSize() is.

-Nate
[ February 28, 2002: Message edited by: Nathan Pruett ]
 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right, I discovered that yesterday. I used getPreferredSize() instead of getSize()and it worked like a charm
thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!