Example usage for javax.swing JTextPane setLogicalStyle

List of usage examples for javax.swing JTextPane setLogicalStyle

Introduction

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

Prototype

public void setLogicalStyle(Style s) 

Source Link

Document

Sets the logical style to use for the paragraph at the current caret position.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();

    Style style = textPane.addStyle(null, null);
    StyleConstants.setForeground(style, Color.red);
    textPane.setLogicalStyle(style);

    style = textPane.addStyle(null, null);
    StyleConstants.setForeground(style, Color.red);
    textPane.setLogicalStyle(style);/* w w w. ja  va2  s . c  o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();

    Style style = textPane.addStyle(null, null);
    StyleConstants.setForeground(style, Color.red);
    textPane.setLogicalStyle(style);

    // Set paragraph style; removes logical style
    style = textPane.addStyle(null, null);
    StyleConstants.setUnderline(style, true);
    textPane.setParagraphAttributes(style, true);
    // paragraph is now underlined, not red
}

From source file:Main.java

public static void main(String[] argv) {
    JTextPane textPane = new JTextPane();

    List list = new ArrayList();

    TabStop[] tstops = (TabStop[]) list.toArray(new TabStop[0]);
    TabSet tabs = new TabSet(tstops);

    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabs);
    textPane.setLogicalStyle(style);
}

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);//w  ww.  j av  a2 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: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);/*from   www . java 2s .  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);
        }

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

From source file:org.colombbus.tangara.EditorFrame.java

/**
 * This method changes the standard size of a TAB for a given JTextPane.
 *
 * @param the/* w  ww.  j a  v a 2s.  c  o m*/
 *            JTextPane where to apply this method.
 */
public void setTabSizeOf(JTextPane textPane) {
    int spacesPerTab = tabSize;
    FontMetrics fm = textPane.getFontMetrics(textPane.getFont());
    int charWidth = fm.charWidth(' ');
    int tabWidth = charWidth * spacesPerTab;

    TabStop[] tabStops = new TabStop[200];

    for (int j = 0; j < tabStops.length; j++) {
        int tab = j + 1;
        tabStops[j] = new TabStop(tab * tabWidth);
    }

    TabSet tabSet = new TabSet(tabStops);

    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabSet);
    textPane.setLogicalStyle(style);
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/** Builds the UI component displaying the exception.*/
public static JTextPane buildExceptionArea() {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);/*w w  w.  j  a v  a 2  s.  co  m*/
    textPane.setEditable(false);

    // Create one of each type of tab stop
    List<TabStop> list = new ArrayList<TabStop>();

    // Create a left-aligned tab stop at 100 pixels from the left margin
    float pos = 15;
    int align = TabStop.ALIGN_LEFT;
    int leader = TabStop.LEAD_NONE;
    TabStop tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a right-aligned tab stop at 200 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_RIGHT;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a center-aligned tab stop at 300 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_CENTER;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a decimal-aligned tab stop at 400 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_DECIMAL;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a tab set from the tab stops
    TabSet tabs = new TabSet(list.toArray(new TabStop[0]));

    // Add the tab set to the logical style;
    // the logical style is inherited by all paragraphs
    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabs);
    textPane.setLogicalStyle(style);
    Style debugStyle = document.addStyle("StyleName", null);
    StyleConstants.setForeground(debugStyle, Color.BLACK);
    StyleConstants.setFontFamily(debugStyle, "SansSerif");
    StyleConstants.setFontSize(debugStyle, 12);
    StyleConstants.setBold(debugStyle, false);
    return textPane;
}