Win a copy of Penetration Testing Basics this week in the Security forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Painting background in JComponent

Scott Palmer
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having some trouble understanding what is going on with JComponent painting.
The docs seem to indicate that if the component is opaque that the background is painted with the background colour. But I hvae found that JComponent never paints the background properly!
I have made my own component that derives from JComponent.
I override the PaintComponent method and the first line of my paintComponet calls super.paintComponent. I also called setOpaque(true) in the constructor, and just to be safe override isOpaque to always return false.
when my component is painted the "background" is painted with garbage graphics left over from the last thing that painted. I expected it to be filled with the background colour. If I do not make the compinent opaque then I do get a clean background colour of my frame showing through. So clearly JComponent is painting something for the background - but it is not working. This happens in 1.3.1 and the 1.4 beta.
I can solve my problem by deriving from JPanel instead of JComponent or by explicitly painting the background myself in the paintComponent methoid.. But I would like to understand why Jcomponent is behaving this way.
Thanks in advance for your help,
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic