Example usage for javax.swing JTextPane addStyle

List of usage examples for javax.swing JTextPane addStyle

Introduction

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

Prototype

public Style addStyle(String nm, Style parent) 

Source Link

Document

Adds a new style into the logical style hierarchy.

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);/*from   www  .  j a va  2s. c  o  m*/

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

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);/* ww  w  . j  ava2s .  c  om*/

    // 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) throws Exception {
    JTextPane textPane = new JTextPane();
    StyledDocument doc = textPane.getStyledDocument();

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

    // Inherits from "Red"; makes text red and underlined
    style = textPane.addStyle("Red Underline", style);
    StyleConstants.setUnderline(style, true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JTextPane textPane = new JTextPane();
    StyledDocument doc = textPane.getStyledDocument();

    Style style = textPane.addStyle("I'm a Style", null);
    StyleConstants.setForeground(style, Color.red);

    doc.insertString(doc.getLength(), "BLAH ", style);

    StyleConstants.setForeground(style, Color.blue);

    doc.insertString(doc.getLength(), "BLEH", style);
}

From source file:Main.java

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

    // Makes text red
    Style style = textPane.addStyle("Red", null);
    StyleConstants.setForeground(style, Color.red);

}

From source file:Main.java

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

    // Makes text 24pts
    Style style = textPane.addStyle("24pts", null);
    StyleConstants.setFontSize(style, 24);
}

From source file:Main.java

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

    // Makes text italicized
    Style style = textPane.addStyle("Italic", null);
    StyleConstants.setItalic(style, true);
}

From source file:Main.java

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

    // A style can have multiple attributes; this one makes text bold and italic
    Style style = textPane.addStyle("Bold Italic", null);
    StyleConstants.setBold(style, true);
    StyleConstants.setItalic(style, true);
}

From source file:Main.java

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

    StyleContext styleContext = new StyleContext();
    c1.setDocument(new DefaultStyledDocument(styleContext));
    c2.setDocument(new DefaultStyledDocument(styleContext));

    Style style = c1.addStyle("style name", null);
    StyleConstants.setForeground(style, Color.red);

    style = c2.getStyle("style name");
    StyleConstants.setBold(style, true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();

    JButton button = new JButton("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel.setLayout(new BorderLayout());
    panel.setPreferredSize(new Dimension(200, 200));

    panel.add(textPane, BorderLayout.CENTER);
    panel.add(button, BorderLayout.SOUTH);
    textPane.addStyle("myStyle", null);

    button.addActionListener(e -> {//from   ww w  .  j  av a 2 s . com
        StyledDocument doc = textPane.getStyledDocument();
        int start = textPane.getSelectionStart();
        int end = textPane.getSelectionEnd();
        if (start == end) {
            return;
        }
        if (start > end) {
            int life = start;
            start = end;
            end = life;
        }
        Style style = textPane.getStyle("myStyle");

        if (StyleConstants.isBold(style)) {
            StyleConstants.setBold(style, false);
        } else {
            StyleConstants.setBold(style, true);
        }
        doc.setCharacterAttributes(start, end - start, style, false);
    });

    frame.add(panel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}