aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing: Why this method execute without being called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing: Why this method execute without being called" Watch "Swing: Why this method execute without being called" New topic
Author

Swing: Why this method execute without being called

Ken Nguyen
Greenhorn

Joined: Apr 25, 2013
Posts: 3
Hi All

Please explain the following code for me, why the "paint" method execute without being called?

Thank you
Ken

========
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
It is getting called, just not by your code. Just like the main method runs without being called from within your code.

You should not override the paint method, by the way, if you use Swing components, but paintComponent. See http://www.oracle.com/technetwork/java/painting-140037.html and http://docs.oracle.com/javase/tutorial/uiswing/painting/ for details.
Ken Nguyen
Greenhorn

Joined: Apr 25, 2013
Posts: 3
Thanks for reply, as I am quite new to Java, please explain it more detail. As I understand, main method is the default method to run in Java project. How about in this case? In which way it is getting called?? BTW I not write this code myself
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
The paintComponent method is the method that gets called whenever the JVM decides that a Swing component should be drawn. It will in turn cause paint to be called, but as I said, you should not write code that way. If you're learning Swing, I suggest to start by changing this code to override paintComponent instead of paint (and, of course, read the links I posted if you haven't done so yet).
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
please explain it more detail

If you want to know the details, then you need to do the reading yourself. Ulf, gave you two execellent links that explain how painting works. So take the time to read them both. The first gets into the technical detail of painting, the second is more practical and gives you an example to learn from. Once you read the articles if there is something you don't understand then you can ask a specific question about something you read, but we are not going to read and summarize the article for you.

The paintComponent method is the method that gets called whenever the JVM decides that a Swing component should be drawn. It will in turn cause paint to be called


Actually it is the other way around paint() will invoke paintComponent(). See: A Closer Look at the Painting Mechanism

Basically
Ken Nguyen
Greenhorn

Joined: Apr 25, 2013
Posts: 3
Thank you all guy, that is really helpful for me. I really appreciate it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing: Why this method execute without being called