• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing: Why this method execute without being called

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

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

Thank you
Ken

========
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all guy, that is really helpful for me. I really appreciate it
 
reply
    Bookmark Topic Watch Topic
  • New Topic