Reputation: 69
I am using jTextPane to use sender and receiver chat color. All works fine but javax.swing.text.DefaultStyledDocument@123456
with every chat message.
here Jhon is revceiver and peter is sender
here peter is revceiver and Jhon is sender
may be I m doing some mistake in code.
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss\n\t dd/MM/yyyy ");
Date date = new Date();
StyledDocument doc = GUI.jTextPane1.getStyledDocument();
Style style = GUI.jTextPane1.addStyle("a style", null);
StyleConstants.setForeground(style, Color.red);
try {
doc.insertString(doc.getLength(), "\t " + "You" + " : " + GUI.getSendMessage() +("\n \t "+dateFormat.format(date)) ,style);
GUI.appendReceivedMessages(""+doc);
}
catch (BadLocationException e){}
DateFormat dateFormate = new SimpleDateFormat("HH:mm:ss\ndd/MM/yyyy ");
Date datee = new Date();
StyledDocument doc1 = GUI.jTextPane1.getStyledDocument();
Style styler = GUI.jTextPane1.addStyle("a style", null);
StyleConstants.setForeground(styler, Color.blue);
try { doc1.insertString(doc1.getLength(),"recevier" ,styler);
GUI.appendReceivedMessages(fromHeader.getAddress().getDisplayName() + " : "
+ new String(request.getRawContent()) +("\n"+dateFormate.format(datee)));
}
catch (BadLocationException e){}
public void appendReceivedMessages(String s) {
try {
Document doce = jTextPane1.getDocument();
doce.insertString(doce.getLength(), s+"\n", null);
} catch(BadLocationException exc) {
}
}
Upvotes: 1
Views: 124
Reputation: 20783
This is so obvious - not sure if qualifies for an answer. Anyway
Why are you doing GUI.appendReceivedMessages(""+doc);
? that is causing the doc
object's default toString
to appear. Hope that helps
EDIT:
so what can I do here
I guess you can do it like this :
Note that StyledDocument
's insertString
API updates the view. Meaning it provides you the output you need on JTextPane
so:
doc.insertString(doc.getLength(), "\t " + "You" + " : " + GUI.getSendMessage() +("\n \t "+dateFormat.format(date)) ,style);
Is sufficient to bring the output on to the text pane. Remove the call to GUI.appendReceivedMessages(""+doc);
I believe your aim is to display the message text on the text pane component - jTextPane1
. you just need to update property of jTextPane1
for that. You do not need to update anything else. If you need to send the text data around, just get the text from that object and pass it around to methods that expects the value : example :
String text = jTextPane1.getDocument()
.getText(0, jTextPane1.getDocument()
.getLength());
aMethodThatExpectsAString(text);
Upvotes: 2