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

voidsetStyle(JTextPane textPane, int start, int length, String name)
set Style
StyledDocument doc = textPane.getStyledDocument();
Style style;
if (name == null) {
    StyleContext context = StyleContext.getDefaultStyleContext();
    style = context.getStyle(StyleContext.DEFAULT_STYLE);
} else
    style = doc.getStyle(name);
doc.setCharacterAttributes(start, length, style, true);
...
voidsetStyle(JTextPane textPane, Style style)
set Style
String fontFamily = StyleConstants.getFontFamily(style);
int fontSize = StyleConstants.getFontSize(style);
int fontStyle = 0;
fontStyle |= (StyleConstants.isItalic(style) ? Font.ITALIC : 0);
fontStyle |= (StyleConstants.isBold(style) ? Font.BOLD : 0);
Font font = new Font(fontFamily, fontStyle, fontSize);
textPane.setFont(font);
textPane.setBackground(StyleConstants.getBackground(style));
...
voidsetTabs(int charactersPerTab, JTextPane textpane)
Sets the tabWidth for a textpane
charactersPerTab--;
FontMetrics fm = textpane.getFontMetrics(textpane.getFont());
int charWidth = fm.charWidth('w');
int tabWidth = charWidth * charactersPerTab;
TabStop[] tabs = new TabStop[10];
for (int j = 0; j < tabs.length; j++) {
    int tab = j + 1;
    tabs[j] = new TabStop(tab * tabWidth);
...
voidsetTextToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)
Set the text pane text.
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, foregroundColor);
aset = sc.addAttribute(aset, StyleConstants.Background, backgroundColor);
textPane.setCharacterAttributes(aset, false);
textPane.setText(text);
voidwriteImage(final JTextPane jtPane, final ImageIcon msg)
write Image
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        Image scaleImage = msg.getImage().getScaledInstance(75, 75, Image.SCALE_DEFAULT);
        msg.setImage(scaleImage);
        jtPane.insertIcon(msg);
});
...