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:
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);
}
dos is the data output stream
and:
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);
}
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)'
Is anybody able to help?
EDIT: Client reading from server (color removed till I find real solution)
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);
I want to add color context to the last line of code.
EdIT - using appendToPane rather than setText(nothing being displayed, when I remove the last setText from appendToPane:
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);
AND:
String messageout="";
messageout=jTextField1.getText();
jTextField1.setText("");
appendToPane(jTextPane1,"\n"+"server:"+messageout,Color.BLUE);
//jTextPane1.setText(jTextPane1.getText()+"\n"+"server:"+messageout);
dos.writeUTF(messageout);
If your TextPane
is set to be not editable (e.g., you have a tp.setEditable(false)
somewhere, then you cannot use operations that edit it. The method replaceSelection()
is an editing method, and thus, instead of doing anything, it just beeps.
So you have opted to replace the entire text of the text pane, which is not considered an editing method. But then, you lose the styling.
Instead of doing either, in a non-editable text pane, you should add to the document that backs the text pane. So, for example, change your appendToPane
like this:
private static 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);
// Get the TextPane's Document
Document doc = tp.getDocument();
int len = doc.getLength();
try {
doc.insertString(len, msg, aset); // Use the `insertString` method of the document.
} catch (BadLocationException e) {
// Nothing. Using the doc length makes sure this exception isn't thrown
}
}