Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Who is calling my paint method?  RSS feed

 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!