Java Utililty Methods JTextPane

List of utility methods to do JTextPane

Description

The list of methods to do JTextPane are organized into topic(s).

Method

voidaddStyle(JTextPane textPane, String name, Color foreground)
add Style
addStyle(textPane, name, foreground, null, false);
voidappendAsGreen(JTextPane pane, String fontName, int fontSize, String s)
append As Green
if (pane.getStyle("greenStyle") == null) {
    Style greenStyle = pane.addStyle("greenStyle", null);
    StyleConstants.setForeground(greenStyle, SUCCESSFUL_GREEN);
    StyleConstants.setFontFamily(greenStyle, fontName);
    StyleConstants.setFontSize(greenStyle, fontSize);
StyledDocument doc = (StyledDocument) pane.getDocument();
try {
...
voidappendAsMessage(JTextPane pane, String fontName, int fontSize, String s)
Appends a given string to the StyledDocument of a pane as a message, that is, using the Style 'MessageStyle'.
if (pane.getStyle("MessageStyle") == null) {
    Style messageStyle = pane.addStyle("MessageStyle", null);
    StyleConstants.setForeground(messageStyle, Color.BLACK);
    StyleConstants.setFontFamily(messageStyle, fontName);
    StyleConstants.setFontSize(messageStyle, fontSize);
StyledDocument doc = (StyledDocument) pane.getDocument();
try {
...
voidappendString(String str, JTextPane jtext)
append String
StyledDocument document = (StyledDocument) jtext.getDocument();
document.insertString(document.getLength(), str, null);
voidappendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)
Append some text in one JTextPane, using colors.
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, foregroundColor);
aset = sc.addAttribute(aset, StyleConstants.Background, backgroundColor);
int len = textPane.getDocument().getLength();
textPane.setCaretPosition(len);
textPane.setCharacterAttributes(aset, false);
textPane.replaceSelection(text);
voidapplyRegex(String regex, JTextPane pane, Color highlightColor)
apply Regex
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(pane.getText());
while (matcher.find()) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground,
            highlightColor);
    pane.getStyledDocument().setCharacterAttributes(matcher.start(), matcher.end() - matcher.start(), aset,
            true);
...
voidcenterText(JTextPane pane)
center Text
StyledDocument doc = pane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
JTextPanecreateJTextPane(String text, Color backgroundColor)
Creates a new JTextPane object with the given properties.
JTextPane jTextPane = new JTextPane();
jTextPane.setBorder(null);
jTextPane.setEditable(false);
jTextPane.setBackground(backgroundColor);
jTextPane.setFont(new Font("Times New Roman", Font.PLAIN, 14));
if (text != null) {
    jTextPane.setText(text);
jTextPane.setVerifyInputWhenFocusTarget(false);
jTextPane.setAutoscrolls(false);
return jTextPane;
JScrollPanecreateTextPaneScrollPane(JTextPane textPane)
Creates a default scroll pane for a text pane.
JPanel noWrapPanel = new JPanel(new BorderLayout());
noWrapPanel.add(textPane);
JScrollPane scrollPane = new JScrollPane(noWrapPanel);
scrollPane.getVerticalScrollBar().setUnitIncrement(textPane.getFont().getSize() * 2);
return scrollPane;
StringgetFontFamily(JTextPane textPane)
Returns the font family of the current character.
String family = (String) (textPane.getInputAttributes().getAttribute(StyleConstants.FontFamily));
return family;