swing - java update Jpanel component -


i using custome jpanel in gui builder jfram class a, problem facing update components (lable) in jpanel when click button in jframe.here button in gui builder jframe classa: changes color of jpl , remove labels not update new labels.

private void btnshowactionperformed(java.awt.event.actionevent evt) {     // todo add handling code here:              random randomgenerator = new random();             (int idx = 1; idx <= 10; ++idx) {                 q = randomgenerator.nextint(100);             }             jpl1.removeall();             new jpl().printme(classa.q);             jpl1.revalidate();             jpl1.setbackground(color.blue);             jpl1.repaint(); } 

here jpl class used custome component in guibuilder jframe class a.

public class jpl extends jpanel {  public jpl() {     printme(classa.q); }   public void printme(int q) {      (int = 0; <q; i++) {         system.out.println(i+"rinting lable");         string htmllabel = "<html><font color=\"#a01070\">" + + " new lable </font></html>";         jlabel lbl = new jlabel(htmllabel);         setlayout(new gridlayout(0, 1));         add(lbl, jpl.right_alignment);         lbl.setforeground(color.blue);         border border = borderfactory.createlineborder(color.lightgray);         lbl.setborder(border);         lbl.add(new jseparator(swingconstants.horizontal));          lbl.addmouselistener(new mouseadapter() {              @override             public void mousepressed(mouseevent e) {                 jlabel label = (jlabel) e.getsource();                 joptionpane.showmessagedialog(null, "you slected");                 system.out.println(label.gettext() + "no akka selected");             }         });     }  } 

you calling printme() on new instance of jpl, try that:

private void btnshowactionperformed(java.awt.event.actionevent evt) {     // todo add handling code here:              random randomgenerator = new random();             (int idx = 1; idx <= 10; ++idx) {                 q = randomgenerator.nextint(100);             }             jpl1.removeall();             jpl1.printme(classa.q); // here - removed new , using jpl1 instance             jpl1.setbackground(color.blue);             jpl1.revalidate();             jpl1.repaint(); } 

in don't understand why loop 10 times random number. last result kept, maybe wanted use q += randomgenerator.nextint(100);. also, classa.q should replaced q if it's same variable.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -