Java Reference
In-Depth Information
JTextPane textPane = new JTextPane(document);
textPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textPane);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
In addition to specifying a position and alignment, you can specify which character you
want to appear as a leader in the white space created by the tab character. By default, nothing
exists there; therefore, the constant is
LEAD_NONE
. The other
TabStop
values create a line of
periods (dots), equal signs, hyphens, thick lines, or underlines:
LEAD_DOTS
,
LEAD_EQUALS
,
LEAD_HYPHENS
,
LEAD_THICKLINE
, or
LEAD_UNDERLINE
. Unfortunately, this option is available but
unsupported. While a nonstandard Swing component
might
support this capability, the standard
ones currently do not support the different leader settings.
Style Interface
The
Style
interface is one more of the enhanced ways to specify an
AttributeSet
. It adds a
name to the
MutableAttributeSet
and the ability to attach a
ChangeListener
to a
Style
in order
to monitor changes to the attribute settings. For instance, you can configure a bold-italic style
as shown here:
String BOLD_ITALIC = "BoldItalic";
Style style = (Style)document.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setBold(style, true);
StyleConstants.setItalic(style, true);
document.addStyle(BOLD_ITALIC, null);
Later, you can attach the style to some text:
style = document.getStyle(BOLD_ITALIC);
document.insertString(document.getLength(), "Hello, Java", style);
StyleContext Class
The
StyleContext
class manages the styles for a styled document. With the help of the
StyleContext.NamedStyle
class, you can just let the
JTextPane
to do its own thing, because
the
StyleContext
knows when something needs to be done. For HTML documents, the
StyleContext
is more specifically a
StyleSheet
.