Java jTextPane one line with regular and bold Text missplaced -


i have textpane using styleddocument. if message typed first adds current time , other users ip document after custom message entered user added right behind in bold.

apparently problem bold takes more space , makes misplaced resulting in this: enter image description here <- code used process following:

public void addtext(string msg) {    	long timems = system.currenttimemillis();  	date instant = new date(timems);  	simpledateformat sdf = new simpledateformat("hh:mm");  	string time = sdf.format(instant);  	simpleattributeset bold = new simpleattributeset();  	styleconstants.setbold(bold, true);  	try {  	    styleddocument doc = getchat().getstyleddocument();  	    doc.insertstring(doc.getlength(), time + ": " + client.getclient().getpartnerip() + " >>", null);  	    doc.insertstring(doc.getlength(), msg + "", bold);  	    jscrollbar sb = getchatscroller().getverticalscrollbar();  	    sb.setvalue(sb.getmaximum());  	} catch (badlocationexception e) {  	    e.printstacktrace();  	}      }

i know fixed using htmleditorkit, don't want use htmleditorkit.

eventho said don't want use htmleditorkit used because fix find.

this current working method:

public void addmsg(string msg, string from) { string formattedmessage = string.format("%s%s<font color=#ff0000 size=5><b>%s</b></font>\n", from, (from == getusername() ? " >>" : " &lt;&lt;"), msg); addtext(formattedmessage, true); } 

and dont forget set editorkit on textpane!

chat.seteditorkit(new htmleditorkit()); 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -