A rather dull worKLOG. This is just a scratchpad for solutions to IT problems that might be useful to someone else. Expect no opinions, no brilliant insights and definitely no pictures of pets or children. Expect stack traces, code snippets and other hints for the Google Indexer.

Wednesday, August 23, 2006

How to get an applet to repaint

You have an applet. In response to some event you want to change all the components it contains. However, your changes don't appear...why?
If I was using a JFrame, I'd liberally throw in a few pack() statements, which causes the JFrame to resize itself based on the new components, and redraw, but you don't have this option with an applet. After some javadocing, it seems that a combination of validate(), invalidate() and repaint() ought to do it. Some experiments later, it seems that
  • invalidate does nothing
  • validate shows up the new components, but over the old ones
  • repaint erases everything
  • invalidate followed by validate is the same as validate
  • invalidate followed by repaint is the same as repaint

  • validate followed by repaint does the trick