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

ColorgetForegroundColor(JTextPane textPane)
Returns the current foreground color.
Color color = (Color) (textPane.getInputAttributes().getAttribute(StyleConstants.Foreground));
if (color == null) {
    color = new Color(0, 0, 0);
return color;
SimpleAttributeSetinsertStyledString(JTextPane textPane, String text, SimpleAttributeSet attrSet, Color background, Color foreground, String fontFamily, int fontSize, boolean bold, boolean italic, boolean underline, boolean strikeThrough, Boolean superscript)
insert Styled String
if (attrSet == null) {
    attrSet = new SimpleAttributeSet();
if (background != null)
    StyleConstants.setBackground(attrSet, background);
if (foreground != null)
    StyleConstants.setForeground(attrSet, foreground);
if (fontFamily != null)
...
booleanisSelectionBold(JTextPane textPane)
Returns true if all of the selection is bold, otherwise false.
int selectionStart = textPane.getSelectionStart();
int selectionEnd = textPane.getSelectionEnd();
StyledDocument doc = (StyledDocument) (textPane.getDocument());
if (selectionStart == selectionEnd) {
    AttributeSet attr = textPane.getInputAttributes();
    if (!attr.containsAttribute(StyleConstants.Bold, new Boolean(true))) {
        return false;
} else {
    for (int i = selectionStart; i < selectionEnd; i++) {
        AttributeSet attr = doc.getCharacterElement(i).getAttributes();
        if (!attr.containsAttribute(StyleConstants.Bold, new Boolean(true))) {
            return false;
return true;
voidoverrideStyle(String styleName, JTextPane textPane, Style newStyle)
override Style
Style style = textPane.getStyle(styleName);
if (style == null) {
    style = styleContext.addStyle(styleName, null);
    textPane.addStyle(styleName, style);
overrideStyle(style, newStyle);
voidsaveFile(JTextPane txt)
save File
FileWriter fw;
try {
    fw = new FileWriter(getDiretorio().getAbsoluteFile(), false);
    txt.write(fw);
    fw.close();
} catch (IOException e) {
    e.printStackTrace();
voidscrollTo(JTextPane textPane, int position)
Forces a portion of the document to become visible.
Rectangle r = textPane.modelToView(position);
if (r != null) {
    textPane.scrollRectToVisible(r);
voidsetDefaultTextPaneProperties(JTextPane textPane)
set Default Text Pane Properties
textPane.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, "\n");
voidsetJTextPaneFont(JTextPane jtp, Font font, Color c)
Utility method for setting the font and color of a JTextPane.
MutableAttributeSet attrs = jtp.getInputAttributes();
StyleConstants.setFontFamily(attrs, font.getFamily());
StyleConstants.setFontSize(attrs, font.getSize());
StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0);
StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0);
StyleConstants.setForeground(attrs, c);
StyledDocument doc = jtp.getStyledDocument();
doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false);
...
voidsetSelectionFontSize(JTextPane textPane, int fontSize)
Sets the font size of the selected text.
String FONTFAMILY = "fontSize." + fontSize;
Style fontSizeStyle = new StyleContext().addStyle(FONTFAMILY, null);
fontSizeStyle.addAttribute(StyleConstants.FontSize, new Integer(fontSize));
textPane.setCharacterAttributes(fontSizeStyle, false);
voidsetSelectionForeground(JTextPane textPane, Color color)
Sets the foreground color of the selection.
if (color != null) {
    String FOREGROUND = "foregroundColor." + color.toString();
    Style foregroundStyle = new StyleContext().addStyle(FOREGROUND, null);
    foregroundStyle.addAttribute(StyleConstants.Foreground, color);
    textPane.setCharacterAttributes(foregroundStyle, false);