Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

New to SWING/AWT : paintcomponent called twice by default?  RSS feed

 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am a newby to this but i have a simple question for the below simple code. Why is the paintcomponent called twice by default when it is run?
 
Zee Tuff
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only see it called once when I run your code
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags. Also, don't forget to call super.paintComponent(g) at the top of your paintComponent method.

As for why it's called twice, no idea.
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. Just curious what's the purpose of using code tag?
I am trying to attachment a worddoc file showing a screen shot but it wont allows me.
My console does show the word 'Test' twice.
Please advice.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kim Ming Yap wrote:Thanks for your reply. Just curious what's the purpose of using code tag?

It will show your code with syntax highlighting, and without removing all the spaces and tabs. As a result, it is better to read.
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks.

Further to this, i ran the same code on another laptop and it shows 1 word 'test' while my current laptop which i am using show 2. Any possible reasons?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if it matters? For the most part, Swing decides when things need to be painted. This process is most likely a bit dependent on the platform java is running on. So if at times it gets called twice, Swing thought it needed to repaint. I don't think it is cause for concern.
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kim Ming Yap wrote:Why is the paintcomponent called twice by default when it is run?


Is it a problem if it is called more than once?

Mr. Bolinger touched on this already, but it will be called whenever it thinks it needs to be repainted. You can probably force paintComponent() to be called again by obscuring it with a foreign window, then bringing its window back on top again (or not--the OS may be caching the windows image or something).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!