Example usage for javax.swing JTextPane setCharacterAttributes

List of usage examples for javax.swing JTextPane setCharacterAttributes

Introduction

In this page you can find the example usage for javax.swing JTextPane setCharacterAttributes.

Prototype

public void setCharacterAttributes(AttributeSet attr, boolean replace) 

Source Link

Document

Applies the given attributes to character content.

Usage

From source file:Main.java

public static void main(String... args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");

    JTextPane tPane1 = new JTextPane();
    tPane1.setCharacterAttributes(aset, false);

    f.add(tPane1, BorderLayout.CENTER);

    f.pack();/*w  ww. j av  a  2  s .  c o  m*/
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws BadLocationException {
    JFrame jf = new JFrame("StyledText");
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = jf.getContentPane();

    JTextPane pane = new JTextPane();
    SimpleAttributeSet set = new SimpleAttributeSet();
    StyleConstants.setBold(set, true);

    // Set the attributes before adding text
    pane.setCharacterAttributes(set, true);
    pane.setText("java2s.com ");

    set = new SimpleAttributeSet();
    StyleConstants.setItalic(set, true);
    StyleConstants.setForeground(set, Color.red);
    StyleConstants.setBackground(set, Color.blue);

    Document doc = pane.getStyledDocument();
    doc.insertString(doc.getLength(), "Swing ", set);

    set = new SimpleAttributeSet();
    StyleConstants.setFontSize(set, 24);

    doc.insertString(doc.getLength(), "Tutorial", set);

    JScrollPane scrollPane = new JScrollPane(pane);
    cp.add(scrollPane, BorderLayout.CENTER);

    jf.setSize(400, 300);// ww w.  ja v a  2 s  .  c  om
    jf.setVisible(true);
}

From source file:StylesExample7.java

public static void addText(JTextPane pane, StyleContext sc, Style logicalStyle, Paragraph[] content) {
    // The outer loop adds paragraphs, while the
    // inner loop adds character runs.
    int paragraphs = content.length;
    for (int i = 0; i < paragraphs; i++) {
        Run[] runs = content[i].content;
        for (int j = 0; j < runs.length; j++) {
            pane.setCharacterAttributes(
                    runs[j].styleName == null ? SimpleAttributeSet.EMPTY : sc.getStyle(runs[j].styleName),
                    true);//from ww w  .  ja v  a 2  s  .c o  m
            pane.replaceSelection(runs[j].content);
        }

        // At the end of the paragraph, add the logical style and
        // any overriding paragraph style and then terminate the 
        // paragraph with a newline.
        pane.setParagraphAttributes(SimpleAttributeSet.EMPTY, true);

        if (logicalStyle != null) {
            pane.setLogicalStyle(logicalStyle);
        }

        if (content[i].styleName != null) {
            pane.setParagraphAttributes(sc.getStyle(content[i].styleName), false);
        }

        pane.replaceSelection("\n");
    }
}

From source file:Main.java

private static void appendToPane(JTextPane tp, String msg, Color f, Color b) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, f);
    aset = sc.addAttribute(aset, StyleConstants.Background, b);

    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
    aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

    int len = tp.getDocument().getLength();
    tp.setCaretPosition(len);//from www  .  j  a v a  2s. c o m
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);
}

From source file:ExtendedParagraphExample.java

public static void addText(JTextPane pane, StyleContext sc, Style logicalStyle, Paragraph[] content) {
    // The outer loop adds paragraphs, while the
    // inner loop adds character runs.
    int paragraphs = content.length;
    for (int i = 0; i < paragraphs; i++) {
        Run[] runs = content[i].content;
        for (int j = 0; j < runs.length; j++) {
            pane.setCharacterAttributes(
                    runs[j].styleName == null ? SimpleAttributeSet.EMPTY : sc.getStyle(runs[j].styleName),
                    true);/* ww w. ja v a 2  s .  com*/
            pane.replaceSelection(runs[j].content);
        }

        // At the end of the paragraph, add the logical style and
        // any overriding paragraph style and then terminate the
        // paragraph with a newline.
        pane.setParagraphAttributes(SimpleAttributeSet.EMPTY, true);

        if (logicalStyle != null) {
            pane.setLogicalStyle(logicalStyle);
        }

        if (content[i].styleName != null) {
            pane.setParagraphAttributes(sc.getStyle(content[i].styleName), false);
        }

        if (i != paragraphs - 1) {
            pane.replaceSelection("\n");
        }
    }
}

From source file:Main.java

private void appendToPane(JTextPane tp, String msg, Color c) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
    aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

    int len = tp.getDocument().getLength();
    tp.setCaretPosition(len);/*  w  w w.  j ava2 s .com*/
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);
}

From source file:com.NewJFrame.java

private void appendToPane(JTextPane tp, String msg, Color c) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

    //        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
    //        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
    int len = tp.getDocument().getLength();
    tp.setCaretPosition(len);//from  w  ww .  j a  v a  2  s  .com
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);

}