multithreading - Do something at variable change in Java -
i want following: specific variable (roomname) changes value, title of jframe should changed new value of roomname. problem is, jframe built before roomname changes.
this little snippet of connection.java class:
public connection() { ... fieldname.addkeylistener(new keylistener() { public void keypressed(keyevent e) { if(e.getkeychar() == keyevent.vk_enter) { setname(); } } }); } public void setname(){ chatframe.frame.setvisible(true); chatframe.roomname = fieldname.gettext(); this.dispose(); }
the other class chatframe.java should described action above. need listener or thread this? what's best way it?
one simple solution springs mind wrap variable object, setter method can this:
public void setnewvalue(string newval) { if(!newval.equals(currentval)) { currentval = newval; // value has changed. call relevant code. } }
Comments
Post a Comment