Java Reference
In-Depth Information
0 12345
6
Swinod.
P
P
P
1
Figure 18.4 As a result of deleting symbols g_is_go the two position markers P 2 and P 3
now occupy the same place. Both have offset 4
Position markers are implemented in interface Position in the javax.swing
.text library. There is only one method
int getOffset()
which returns the current offset of the position marker.
18.2
Text attributes
In a text document, one can assign text attributes to parts of the text. These specify
the text style (bold, italics), size, colour and more.
In Swing, text attributes are provided by interface AttributeSet .Apredefined
implementation of this interface is class SimpleAttributeSet , which we shall use
in the following. Both are found in the library javax.swing.text .Inanattribute
set one can, for example, specify that the text should be bold or italic and have
size (height) 14 points (pixels). Then this attribute set can be assigned to different
text parts and all these parts are displayed in the specified way. We only need the
constructor from this class:
SimpleAttributeSet();
To set the attribute values of an attribute set attrSet one uses methods from
another class, namely StyleConstants .Welist and explain some of them:
setForeground(SimpleAttributeSet attrSet, Color c)
setBackground(SimpleAttributeSet attrSet, Color c)
setFontSize(SimpleAttributeSet attrSet, int size)
setBold(SimpleAttributeSet attrSet, boolean b)
setItalic(SimpleAttributeSet attrSet, boolean b)
setUnderline(SimpleAttributeSet attrSet, boolean b)
setSuperscript(SimpleAttributeSet attrSet, boolean b)
setSubscript(SimpleAttributeSet attrSet, boolean b)
setForeground(SimpleAttributeSet attrSet, Color c) sets the text colour
in attrSet to c . The default is the text colour of the graphical component
which displays the text, usually black.
setBackground(SimpleAttributeSet attrSet, Color c) sets background co-
lour in attrSet to c . The default is the background colour of the graphical
component that displays the text, usually white.
setFontSize(SimpleAttributeSet attrSet, int size) sets font size in
attrSet to size points (pixels). The default is the default size of the graphical
component that displays the text, often 12 or 14 points.
Search WWH ::




Custom Search