java - How to change jlabel text from another class -
java - How to change jlabel text from another class -
hi i'm making app but, found problem. i'm using netbeans gui builder build gui.
so, first gui class has lot of button(every button same function) have actionlistener looks this:
public class guipanel extends jpanel { private void jbtttactionperformed(java.awt.event.actionevent evt) { if(mb.getlevel() > 16){ if(ttp != 20 && mb.getpoints() != 0){ point(); ttp++; jbttt.settext(""+ttp); } } } private void point(){ mb.reducepoints(); } int ttp; base of operations mb = new base(); jbutton jbttt = new jbutton(); } the base of operations class has lot of method 1 related problem looks this:
public class base of operations extends jframe { //point decrement method public void reducepoints(){ points--; jlbpoints.settext("points available: "+points); } //return point value public int getpoints(){ homecoming this.points; } //return level value public int getlevel(){ homecoming this.level; } private static int level = 1; private static int points = 20; private jlabel jlbpoints = new jlabel("points available: "+points); } so problem this, when pressed jbttt points variable decrement value alter 20 19. used system.out.println verify that. can see, reducepoints() method should update jlbpoints text doesnt. have tried making base mb = new base() static base of operations mb = new base() still doesn't work. doing wrong? have suggestion?
edit: i've tried system.out.println(jlbpoints.gettext()); , text did changed. 1 didn't alter text user can see. repaint() , revalidate didn't work.
edit2: found reply question. have pass 'base' class object 'guipanel' class since created 'base' class object in main class(i don't want utilize class main class). passed next method:
public void passobj(base mb){ this.mb = mb; } and changing constructor of 'guipanel' class this:
public guipanel(base mb) { initcomponents(); this.mb = mb; } also changing base mb = new base(); base mb;
i wanted give thanks have tried reply question.
try using swingutilities.invokelater() ensure changing text on event dispatch thread, supposed to. can guess given current information.
java swing jlabel
Comments
Post a Comment