swing - Why does repaint() cause a different cause different results than resizing the window? (Java) -


i'm trying build program displays random shapes , random text. code inside class extends jpanel.

@override protected void paintcomponent(graphics g) {      // draw shapes             super.paintcomponent(g);     // clears background             this.add(new jlabel(new randomtext().getrandomsentence()));     (randomshape shape : shapes) {        shape.draw(g);     }          } 

i use jpanel instead of g.drawstring() in future display html code. have button "regenerates" window, calling repaint() method. however, when push regenerate button, calling repaint() method, shapes change (like they're supposed to), text not. find stranger when resize window new labels continously added panel (with random text they're supposed to).

does super.paintcompnent(g) not clear components added panel? if so, why calling repaint() myself not add labels, resizing window does?

resizing window forces validate() called lays out components on panel again - repainting not.

however, in painting method you're adding new jlabel each time , not removing any, isn't want do. should keep track of jlabel in separate variable , either change it's position when repaint (preferably) or remove before adding new one.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -