Search code examples
javauser-interfacejbuttondistributed-computing

Action Listener doesn't change set variable to other value


I am writing GUI for a chat, and I have problem I can't seem to find a solution.

When button Send is clicked variable OKpressed should change to true and in function getUserInput it should recognize it changed but it doesn't.. It's acting like it still says false.. I tried printing out in Send that works, so problem is only that functiong getUserInput doesn't recognize variable as changed

Any help is appreciated..Here's the code I can't attach all other classes so you can start it, but everything is working except the problem mentioned above

public class Chat extends Process {

public static class myFrame extends JFrame{

/** Creates a 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 from " + 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 with -1 at end:");
    System.out.println("Only one pid for synch order:");
    IntLinkedList destIds = new IntLinkedList(); //dest for 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);
    for (int i = 0; i < 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);
    }
}
}

Solution

  • As you wrote, I cannot run you code, so it is kind of guess, however I think the problem is in empty infinite loop:

     while (t.OKPressed == false){}
    

    if you add anything inside, even:

    while(t.OKPressed == false){
         System.out.println();
    }
    

    It should work. It is connected with problem better described for example here: Threads: Busy Waiting - Empty While-Loop, and in post which duplicate it is.