my Reader class is used to display server messages whenever the server sends one. This code works perfectly when printing directly to the command line, I receive ever message sent by the server. However, the JTextArea only shows every other line or so. I am not sure what to do here. I read something about InvokeLater, but I am unsure if this is the correct situation to use it.
Also, the Thread is started from a GUI class when I click the connect button.
public class Reader implements Runnable {
public static Socket s = null;
public static JTextArea TextArea;
public static BufferedReader reader = null;
/*Takes in a Socket. Sets up a input stream.
*
*/
public Reader(Socket sIn, JTextArea display) {
TextArea=display;
s = sIn;
try {
reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (IOException ex) {
System.out.println("Error in constructor Reader: " + ex);
}
}
public void checker() {
try {
/*Sits and waits for a Server Response*/
while (!reader.ready()) {}
System.out.println(reader.readLine());
TextArea.append(reader.readLine()+"\n");
} catch (Exception ex) {
System.out.println("checker is crying: " + ex);
}
}
public void run() {
while (true) {
checker();
}
}
}
Thank you for your replies and examples. I have added the following to the checker() method :
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TextArea.append(reader.readLine() + "\n");
} catch (Exception eee) {
System.out.println("Error" + eee);
}
}
});
The same problem exists (receives only every other server message) but now it locks up on every other message. If my server send 4 messages, it skips the first one gets the second etc.. Thank you for your help!