title: How to make color coded chats between client and server using JTextPane?
body: <p>I have a simple client-server chat system that I want to colour code so that messages from the client and messages fom the server get displayed in different colors. I have the following:</p>
  
  <pre><code>       try {
              String messageout="";
             messageout=jTextField1.getText();
             jTextField1.setText("");
  
          appendToPane(jTextPane1,"\n"+"client: "+messageout,Color.BLUE);
          dos.writeUTF(messageout);
      } catch (IOException ex) {
          Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
      }
  </code></pre>
  
  <p>dos is the data output stream</p>
  
  <p>and:</p>
  
  <pre><code>private void appendToPane(JTextPane tp, String msg, Color c)
  {
      StyleContext sc = StyleContext.getDefaultStyleContext();
      AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
  
      aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
      aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
  
      int len =tp.getDocument().getLength();
      tp.setCaretPosition(len);
      tp.setCharacterAttributes(aset, false);
      tp.replaceSelection(msg);
      tp.setText(tp.getText()+msg);
  </code></pre>
  
  <p>}</p>
  
  <p>The server has simlar code that sets the color to green rather than blue. The problem is I want that client messages to be displayed in blue and the server messages to be displayed in green, whereas at the moment, in the client, all messages are being displayed in blue and in the server all messages are being displayed in green. I want the following:
  'client:blaablaa(in blue)'
  'server:blaablaa(in green)'</p>
  
  <p>Is anybody able to help?</p>
  
  <p>EDIT: Client reading from server (color removed till I find real solution)</p>
  
  <pre><code>        s=new Socket("localhost",1000);
          dis=new DataInputStream(s.getInputStream());
          dos=new DataOutputStream(s.getOutputStream());
          while(!msgin.equals("bye")){
              msgin=dis.readUTF();
             jTextPane1.setText(jTextPane1.getText()+"\n"+"server:"+msgin);
  </code></pre>
  
  <p>I want to add color context to the last line of code.</p>
  
  <p>EdIT - using appendToPane rather than setText(nothing being displayed, when I remove the last setText from appendToPane:</p>
  
  <pre><code>    ss = new ServerSocket(1000);
      s = ss.accept();
      dis=new DataInputStream(s.getInputStream());
      dos=new DataOutputStream(s.getOutputStream());
      while(!msgin.equals("bye")){
          msgin=dis.readUTF();
           appendToPane(jTextPane1,"\n"+"client: "+msgin,Color.RED); 
  </code></pre>
  
  <p>AND:</p>
  
  <pre><code>        String messageout="";
          messageout=jTextField1.getText();
  
  
      jTextField1.setText("");
       appendToPane(jTextPane1,"\n"+"server:"+messageout,Color.BLUE);
       //jTextPane1.setText(jTextPane1.getText()+"\n"+"server:"+messageout);
          dos.writeUTF(messageout);
  </code></pre>