• Post Reply Bookmark Topic Watch Topic
  • New Topic

Painting a JFrame vs Adding components to it

 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy.
I've noticed that if I implement a paint method in a JFrame and run it, any and all components that were added to the frame do not appear in favor of the painting. Would it be true to say that painting overrides components on a JFrame (and elsewhere) or am I missing something?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should NOT be overriding the paint() method. Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you add the panel to the frame.

Read the section from the Swing tutorial on Custom Painting for more information and examples.

Having said that your problem is that you didn't invoke super.paint(g), so the children don't get painted. But don't do this, read the tutorial and do it properly!!!
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob. I haven't seen super.paint(g) yet. I thought it was kind of odd to see the painting used on a Frame itself. I saw it early on in a youtube series and was trying to figure it out. Maybe the creator is making some kind of point. I'll have to wait and see...
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw it early on in a youtube series


Don't use youtube, use the Swing tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!