• Post Reply Bookmark Topic Watch Topic
  • New Topic

Repainting Swing Components

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I want to repaint a Swing component within program code, (app-triggered painting), do I call the "repaint" method or the "paint component" method?
Does repaint call the "paint component" method?
Should custom painting code for a swing component be placed in the "repaint" method or the "paint component" method?
Thanks for the clarification,
Landon
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should call "repaint()", and put custom painting code in the "paintComponent()". "repaint()" is a final method that is implemented somewhere deep down in the AWT API that does all the system specific stuff, like creating real graphics contexts, scheduling repaints, etc. "repaint()" at some point calls "update()", which calls "paint()", which calls "paintComponent()". In AWT you would override "paint()" to do custom painting. In Swing, you should just override "paintComponent()" because "paint()" is implemented to do additional things in Swing. (double-buffering, painting borders & children, etc.).
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that for lightweight components repaint does not call update. And this is one of the reasons why incremental painting should not be used with lightweitgh components.
In this example the click of the button calls repaint but there is no signal of a call to update:

[ March 07, 2004: Message edited by: Jose Botella ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!