Java Reference
In-Depth Information
setBold(SimpleAttributeSet attrSet, boolean b)
determines whether the
text formatted according to
attrSet
should be
bold
(
b=true
)ornot (
b=
false
). The default value is
b=false
.
setItalic(SimpleAttributeSet attrSet, boolean b)
determines whether
the text formatted according to
attrSet
should be in
italics
(
b=true
)or
not (
b=false
). The default value is
b=false
.
setUnderline(SimpleAttributeSet attrSet, boolean b)
determines
whether the text formatted according to
attrSet
should be
underlined
(
b=true
)ornot (
b=false
). The default value is
b=false
.
setSuperscript(SimpleAttributeSet attrSet, boolean b)
determines
whether the text formatted according to
attrSet
should appear as superscript
(
b=true
)ornot (
b=false
). The default value is
b=false
.
setSubscript(SimpleAttributeSet attrSet, boolean b)
determines
whether the text formatted according to
attrSet
should appear as sub-
script (
b=true
)ornot (
b=false
). The default value is
b=false
.
Once an attribute set is defined, it can be assigned to parts of the text. The assign-
ment is done using the following method from class
Document
:
setCharacterAttributes(int offset, int length,
AttributeSet attrSet, boolean replace)
This formats the text part consisting of as many symbols as the current value of
length
and beginning with the symbol at place
offset
as specified by the attribute
values in
attrSet
. The parameter
replace
determines whether the attributes
previously assigned to that text part should be removed first.
In the following example we define an attribute set
ugly
for formatting a text
part in yellow, italic font on red background. Then the text in the document
doc
from symbols
203
to
234
(32 symbols) is formatted in this way. Another attribute
set
greenText
just defines the font colour to be green. This is then used to format
symbols 200 to 214. By setting the parameter
replace
to
false
the attributes of
ugly
other than the colour are maintained where the ranges overlap (symbols 203
to 214). In this range we have green, italic text on a red background. The reader
is encouraged to try this and other examples of overlapping text attributes.
//
Define attribute set ”ugly” and ...
SimpleAttributeSet ugly =
new
SimpleAttributeSet();
// ...
set its attributes to yellow italics
on red
StyleConstants.setForeground(ugly, Color.yellow);
StyleConstants.setBackground(ugly, Color.red);
StyleConstants.setItalic(ugly,
true
);
//
Format 32 symbols beginning with 203 using ”ugly”.
//
Any previously assigned attributes are erased.
styledDoc.setCharacterAttributes(203,32,ugly,
true
);
Search WWH ::
Custom Search