java - jTextPane color with some exception in chat -
i using jtextpane use sender , receiver chat color. works fine javax.swing.text.defaultstyleddocument@123456
with every chat message.
here jhon revceiver , peter sender
here peter revceiver , jhon sender
may m doing mistake in code.
here code sender
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){}
here code receiver
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){}
here main gui these
public void appendreceivedmessages(string s) { try { document doce = jtextpane1.getdocument(); doce.insertstring(doce.getlength(), s+"\n", null); } catch(badlocationexception exc) { } }
this obvious - not sure if qualifies answer. anyway
why doing gui.appendreceivedmessages(""+doc);
? causing doc
object's default tostring
appear. hope helps
edit:
so can here
i guess can : note styleddocument
's insertstring
api updates view. meaning provides output need on jtextpane
so:
doc.insertstring(doc.getlength(), "\t " + "you" + " : " + gui.getsendmessage() +("\n \t "+dateformat.format(date)) ,style);
is sufficient bring output on text pane. remove call gui.appendreceivedmessages(""+doc);
i believe aim display message text on text pane component - jtextpane1
. need update property of jtextpane1
that. not need update else. if need send text data around, text object , pass around methods expects value : example :
string text = jtextpane1.getdocument() .gettext(0, jtextpane1.getdocument() .getlength()); amethodthatexpectsastring(text);
Comments
Post a Comment