231

I'm putting together a Swing application where I often want to replace the contents of a JPanel. To do this, I'm calling removeAll(), then adding my new content, then calling revalidate().

However I'm finding that the old content is still actually visible (though obscured by the the new content). If I add a call to repaint() in addition to revalidate(), it works as expected.

I'm sure on other occasions I've experienced that just calling revalidate() is enough.

So basically my question is - should I need to call both functions and if not, when should I call each of them?

Ben
  • 54,723
  • 49
  • 178
  • 224
William
  • 13,332
  • 13
  • 60
  • 73
  • 8
    [This tutorial from Oracle](http://docs.oracle.com/javase/tutorial/uiswing/components/jcomponent.html#custompaintingapi) states: "Always invoke repaint after revalidate". No explanation is provided though. – mins Oct 29 '14 at 19:39

5 Answers5

176

You need to call repaint() and revalidate(). The first one tells Swing that an area of the window is dirty (which is necessary to erase the image of the old children removed by removeAll()); the second one tells the layout manager to recalculate the layout (which is necessary when adding components). This should cause children of the panel to repaint, but may not cause the panel itself to do so (see this for the list of repaint triggers).

On a more general note: rather than reusing the original panel, I'd recommend building a new panel and swapping them at the parent.

wolfenblut
  • 131
  • 1
  • 11
kdgregory
  • 38,754
  • 10
  • 77
  • 102
91

Any time you do a remove() or a removeAll(), you should call

  validate();
  repaint();

after you have completed add()'ing the new components.

Calling validate() or revalidate() is mandatory when you do a remove() - see the relevant javadocs.

My own testing indicates that repaint() is also necessary. I'm not sure exactly why.

Noel Grandin
  • 3,143
  • 25
  • 17
63

revalidate is called on a container once new components are added or old ones removed. this call is an instruction to tell the layout manager to reset based on the new component list. revalidate will trigger a call to repaint what the component thinks are 'dirty regions.' Obviously not all of the regions on your JPanel are considered dirty by the RepaintManager.

repaint is used to tell a component to repaint itself. It is often the case that you need to call this in order to cleanup conditions such as yours.

akf
  • 38,619
  • 8
  • 86
  • 96
7

revalidate() just request to layout the container, when you experienced simply call revalidate() works, it could be caused by the updating of child components bounds triggers the repaint() when their bounds are changed during the re-layout. In the case you mentioned, only component removed and no component bounds are changed, this case no repaint() is "accidentally" triggered.

Poorna Senani Gamage
  • 1,246
  • 2
  • 19
  • 30
Antony Ng
  • 767
  • 8
  • 16
-4

yes you need to call repaint(); revalidate(); when you call removeAll() then you have to call repaint() and revalidate()

Som Adhikari
  • 75
  • 1
  • 1
  • 6