3

When I try to remove Components from a Container, I use this code.

private static void clear(){
    for (int i = con.getComponentCount() - 1; i >= 1; i--){
        con.remove(i);
    }
}

When I call this function, the function acts as if it has done nothing, but crashes it as if its overloading. It gives no error. But when I put con.getComponent(i).setVisible(false); in the code it works, but I want to REMOVE the components. Halp?

Community
  • 1
  • 1
KeirDavis
  • 665
  • 2
  • 10
  • 32

4 Answers4

3

Try using this:

while (con.getComponentCount()>0) {
    con.remove(0);
}
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
2

After you are done removing the components you want to remove, call Container.validate(); Container.repaint(); Actually, you might want to revalidate more than that.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

Have you tried containerObject.repaint() after removing the components?

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
0

Assuming that con is an awt Container, you can call

con.removeAll();

that removes all contained components at once.

Andrea Parodi
  • 5,534
  • 27
  • 46