java - how can I find default attributes for JTextPane document? -
i'm working jtextpane.
jtextpane pane = new jtextpane(); string content = "i'm line of text displayed in jtextpane"; styleddocument doc = pane.getstyleddocument(); simpleattributeset aset = new simpleattributeset();
if add aset
textpane's document this:
doc.setparagraphattributes(0, content.length(), aset, false);
nothing visible happens. no big surprise since haven't set custom attributes aset
. however, if allow aset
replace current paragraphattributes of doc
this:
doc.setparagraphattributes(0, content.length(), aset, true);
a lot of things happen. how can information on default values of jtextpane document? particularly problems when i'm defining custom font aset
, set replace current attributes, font displayed if bold. styleconstants.setbold(aset, false);
doesn't help.
i have looked @ source code see data structures holding information want. modification of code prints attributes each paragraph.
int offset, length; //the value of first 2 parameters in setparagraphattributes() call element section = doc.getdefaultrootelement(); int index0 = section.getelementindex(offset); int index1 = section.getelementindex(offset + ((length > 0) ? length - 1 : 0)); (int = index0; <= index1; i++) { element paragraph = section.getelement(i); attributeset attributeset = paragraph.getattributes(); enumeration keys = attributeset.getattributenames(); while (keys.hasmoreelements()) { object key = keys.nextelement(); object attribute = attributeset.getattribute(key); //system.out.println("key = " + key); //for other attributeset classes line useful because shows actual parameter, "bold" system.out.println(attribute.getclass()); system.out.println(attribute); } }
the output simple textpane text added through settext()
method gives:
class javax.swing.text.stylecontext$namedstyle namedstyle:default {foreground=sun.swing.printcoloruiresource[r=51,g=51,b=51],size=12,italic=false,name=default,bold=false,font_attribute_key=javax.swing.plaf.fontuiresource[family=dialog,name=dialog,style=plain,size=12],family=dialog,}
about particular problem, looking @ related question have been able set text of paragraph bold with:
stylecontext sc = stylecontext.getdefaultstylecontext(); attributeset aset = sc.addattribute(aset, styleconstants.bold, true);
in case class of aset
javax.swing.text.stylecontext$smallattributeset
not mutable (does not implement mutableattributeset
). case along lines:
aset.addattribute(styleconstants.bold, true);
should work.
Comments
Post a Comment