I'm making an instant messaging program with a server and a client. The server and client both send messages through a method in separate projects that look like this:
private void sendMessage(String message){
try{
output.writeObject("CLIENT/SERVER - " + message);
output.flush();
showMessage("\nCLIENT/SERVER - " + message);
}catch(IOException ioException){
chatWindow.append("\n SYSTEM - Something went wrong!");
}
}
and this:
private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(m);
}
}
);
}
All the messages sent by both the client and the server show up in the same JTextArea. I have two questions. First, how do I make it so that each message from the server and the client show up as a different color?
Also, second question, how do I code this so that everything gets appended at the top of the chatWindow rather than the bottom of the chatWindow? Thanks.