Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HFJ and paintComponent

 
Bob Beerbower
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HFJ the following statement is made
The paintComponent() method is called bu the GUI system. You never call it yourself.

The book then presents the following code

If the above code is not calling the paintComponent() method, then what is its purpose
[ March 01, 2006: Message edited by: Bob Beerbower ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The paintCompoment method (it's usually left protected, by the way) is typically not called directly, except in the following idiom:

So how does it get used? Your code may call the repaint() methods (directly or indirectly) these methods return immediately but queue a repainting request. Later the EDT (event dispatch thread) dequeues this request and carries it out, by calling the components paint method. The typical implementation of paint calls subroutines paintComponent, paintBorder and paintChildren, in that order, to do their varied painting tasks. The methods are examples of the template method design pattern. Remember that when you write Swing code you are working in a framework -- you are often providing the subroutines and Swing the overarching code, so it's not suprising that you will need to write methods that you don't call directly...
 
Bob Beerbower
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, thanks for the reply. So the PaintComponent() method in my code is basically telling the system that I need some paint methods to be run, but that its up to the system which ones to run and when?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bob Beerbower:
Jeff, thanks for the reply. So the PaintComponent() method in my code is basically telling the system that I need some paint methods to be run, but that its up to the system which ones to run and when?


Your wording is a little vague: "I need some paint methods to be run" sounds like a request to run the methods, versus stipulating *what* should be done *if* the need to paint arises. Also, "the system" sounds ominous, or could be mistaken for the operating system, but I know what you mean: "the system" == "the Swing GUI framework".

I would put it like this:
1. Calling method repaint (directly or indirectly) is how you get a component to render itself. One way to call repaint indirectly is to make a top-level window visible.
2. Certain methods are called by Swing when a component is rendering itself, including paintComponent. If you want to customize the rendering of a component, override paintComponent and put your custom code there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!