Hi,
I'm trying to construct a simple chat program (for learning purposes). I'm using the NetBeans IDE to construct a GUI with Swing components. I've run into a problem with printing text to a JTextPane:
Essentially, I have an actionPerformed method that contains instructions to print text to the pane, using "replaceSelection" . The program compiles and runs fine, but the text appears late. That is, other events happen first before the text appears.
Details: The problem occurs in a program for a chat Client communicating with a Chat server program. The server program is just a command line thing and works fine. The connection uses socket objects. I took all that code from somewhere else and it works fine.
My chat client has a JFrame that contains a JTextField for input, a "send" button, and a JTextPane for displaying the conversation.
When the client has entered a message, the corresponding actionPerformed method does three things:
1. It prints the message to the JTextPane
2. It transmits the message to the server program on the other machine
3. It disables the "Send" button and calls a "WaitForMessage" routine
The WaitForMessage routine contains an infinite loop ("while(true)") which terminates when a message is received from the server, prints the message to the textPane, and enables the "send" button again.
When the client types a message and clicks "send," the program appears to freeze. No text appears in the textPane until after the server has sent a message back. Once this happens, both the clients message and the server's message appear simultaneously in the textPane.
Does anyone understand why this delay occurs?
Thanks