I add a ActionListener which create a new chat JFrame that can send and receive message from the Server. this is the code in the ActionPerformed() method
BananaChat chat = new BananaChat(name, password, IP, port, status);
try {
chat.chatting();
} catch (Exception e) {
showInfo("fail");
}
so it create a new chat frame, if I didn't invoke the chat.chatting() method, I can send the message to Server normally, But cannot receive the message from server. So I have to invoke this method because I need to keeping listening the message from server if it does send the message.
here is the code of chatting()
String line = null;
try {
while ((line = in.readLine()) != null) {
if (line.equals("your user name is already registered") || line.equals("user name doesn't exist") || line.equals("wrong password")) {
showMessage(line);
break;
}
showMessage(line);
}
} catch (IOException e) {
showMessage("can not receive the message");
}
It is the while loop. If I create my chat frame and invoke this method in the main method, it can work, but if I create the chat frame in ActionListener, it is stuck. it seems that the ActionListener cannot have a while Loop which doesn't end at all.
I don't know how to solve it, is there a better way to create a new chat interface from the login interface?