java - Action Listener doesn't change set variable to other value -


i writing gui chat, , have problem can't seem find solution.

when button send clicked variable okpressed should change true , in function getuserinput should recognize changed doesn't.. it's acting still says false.. tried printing out in send works, problem functiong getuserinput doesn't recognize variable changed

any appreciated..here's code can't attach other classes can start it, working except problem mentioned above

public class chat extends process {  public static class myframe extends jframe{  /** creates new instance of myframe */ private jtextarea chatbox=new jtextarea(10,45); private jscrollpane mychathistory=new jscrollpane(chatbox,jscrollpane.vertical_scrollbar_always,         jscrollpane.horizontal_scrollbar_always); private jtextarea usertext = new jtextarea(5,40); private jscrollpane myuserhistory=new jscrollpane(usertext,jscrollpane.vertical_scrollbar_as_needed,         jscrollpane.horizontal_scrollbar_as_needed); private jbutton send = new jbutton("send"); private jtextfield user=new jtextfield(20); private string servername; private string username; boolean okpressed = false; string poruka; public myframe() {     setresizable(false);      settitle("client");     setsize(560,400);    container cp=getcontentpane();     cp.setlayout(new flowlayout());     cp.add(new jlabel("chat history"));     cp.add(mychathistory);     cp.add(new jlabel("chat box : "));     cp.add(myuserhistory);     cp.add(send);     cp.add(user);      send.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {               poruka=(string)usertext.gettext();              okpressed = true;          }     });    setdefaultcloseoperation(jframe.exit_on_close);     setvisible(true);      } }  static myframe t=new myframe();  public chat(linker initcomm) {     super(initcomm); } public synchronized void handlemsg(msg m, int src, string tag){     if (tag.equals("chat")) {         system.out.println("message " + src +":");         system.out.println(m.getmessage());         t.chatbox.append(src + ":" + m.getmessage() + "\n");      } } public string getuserinput() throws exception {     while (t.okpressed == false){}    string chatmsg=t.poruka;     return chatmsg; } public intlinkedlist getdest(bufferedreader din) throws exception {     system.out.println("type in destination pids -1 @ end:");     system.out.println("only 1 pid synch order:");     intlinkedlist destids = new intlinkedlist(); //dest msg    stringtokenizer st = new stringtokenizer(din.readline());   //    stringtokenizer st = new stringtokenizer(t.poruka);     while (st.hasmoretokens()) {         int pid = integer.parseint(st.nexttoken());         if (pid == -1) break;         else destids.add(pid);     }     return destids; } public static void main(string[] args) throws exception {      string basename = "chat";     int myid = integer.parseint(args[0]);     int numproc = integer.parseint(args[1]);     linker comm = null;         comm = new causallinker(basename, myid, numproc);      chat c = new chat(comm);     (int = 0; < numproc; i++)         if (i != myid) (new listenerthread(i, c)).start();     bufferedreader din = new bufferedreader(     new inputstreamreader(system.in));     while (true){         system.out.println(c.getuserinput());         string chatmsg = c.getuserinput();         if (chatmsg.equals("quit")) break;         t.chatbox.append(myid + ": " + chatmsg +"\n");         intlinkedlist destids =  c.getdest(din);              comm.multicast(destids, "chat", chatmsg);     } } } 

as wrote, cannot run code, kind of guess, think problem in empty infinite loop:

 while (t.okpressed == false){} 

if add inside, even:

while(t.okpressed == false){      system.out.println(); } 

it should work. connected problem better described example here: threads: busy waiting - empty while-loop, , in post duplicate is.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -