4

In Swing, can a child JComponent be rendered outside of the bounds of its parent JComponent, or are children always clipped to the bounds of their parent?

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281

2 Answers2

4

As long as the child is a light-weight component, it will be limited to the boundaries of the parent component. However, if it is too big, It will be cut, no scaling will be done.

If the child is a heavy weight component and the father is light-weight component, the child will be rendered outside of the parent bounds.

There's a really nice article about this here: http://java.sun.com/developer/technicalArticles/GUI/mixing_components/index.html (can't remember who referred me to it but thanks!)

One more update, which I wasn't aware of (in the link):

As of the JDK 6 Update 12 and JDK 7 build 19 releases, it is now possible to seamlessly mix heavyweight and lightweight components within the same container.

MByD
  • 135,866
  • 28
  • 264
  • 277
1

Actually You can reset clip bounds of the Graphics instance used in paint() method. JUst save old clip, set new, do your painting outside the parent bounds and set original clip back.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Will this interfere with Swing's redraw mechanism? Whenever Swing needs to redraw portions of the screen, does it check if the redraw area lies within a parent component, and if not, won't bother to paint its child components? – Tony the Pony May 23 '11 at 09:48
  • As fas as I know it just sets clip bounds. It's up to child component to check clip and repaint only necessary parts – StanislavL May 23 '11 at 13:56