• 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

Who is calling my paint method?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy.
I added a KeyListener to a JFrame and now whenever I preform a Key event, the frame's panel's paint method is invoked. I am not explicitly calling the repaint method so I'm wondering where this is coming from and if I can disable it.

Update: Alright, after some tinkering I found that it was actually just this line in my paint program that was causing it to call itself over and over again:
setFont(new Font("Arial", Font.PLAIN,30));
I'm not sure why this would be but I'll continue to tinker until then.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Swing software will repaint components whenever it considers that to be necessary. So that's who is doing it. And in your example, if you change the font of a component then it's going to have to be redrawn.

And frankly, if having your component's paint() method called is a problem then you haven't done it right. You should expect that paint() will be called at any time no matter how inconvenient, and you should write it so that it doesn't do any important work except painting the component.

But... is it a problem? Why did you ask that question?
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. And you're right, my program involves infinite repainting. I had already incorporated this though, so I was surprised when I noticed that repainting was happening regardless of wether or not my method was called. I felt the need to make sure nothing wrong was happening. If I can safely recall repaint within the paint method like this, maybe I should just do it this way moving forward.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> If I can safely recall repaint within the paint method like this, maybe I should just do it this way moving forward.

No you should NOT be calling repaint() within any painting method.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should only be calling a component's repaint method if you changed something about that component which requires it to be redisplayed. And since you shouldn't be doing anything in a component's paint method except drawing the component, it follows that you shouldn't be calling any component's repaint method there. In other words, what Rob Camick just said.

So you're doing something wrong.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic