Example usage for javax.swing JTextPane getStyle

List of usage examples for javax.swing JTextPane getStyle

Introduction

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

Prototype

public Style getStyle(String nm) 

Source Link

Document

Fetches a named non-null style previously added.

Usage

From source file:Main.java

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

    // Set text in the range [5, 7) red
    doc.setCharacterAttributes(5, 2, textPane.getStyle("Red"), true);
}

From source file:Main.java

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

    // Italicize the entire paragraph containing the position 12
    doc.setParagraphAttributes(12, 1, textPane.getStyle("Italic"), 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 w  w  w. j ava  2  s  . c  o  m
        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);
}

From source file:DocumentModel.java

public static void main(String[] args) {
    final StyledDocument doc;
    final JTextPane textpane;

    JFrame f = new JFrame();

    f.setTitle("Document Model");

    JToolBar toolbar = new JToolBar();
    JButton boldb = new JButton("bold");
    JButton italb = new JButton("italic");
    JButton strib = new JButton("strike");
    JButton undeb = new JButton("underline");

    toolbar.add(boldb);// w  w w.j a v  a  2s .c om
    toolbar.add(italb);
    toolbar.add(strib);
    toolbar.add(undeb);

    f.add(toolbar, BorderLayout.NORTH);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    JScrollPane pane = new JScrollPane();
    textpane = new JTextPane();
    textpane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    doc = textpane.getStyledDocument();

    Style style = textpane.addStyle("Bold", null);
    StyleConstants.setBold(style, true);

    style = textpane.addStyle("Italic", null);
    StyleConstants.setItalic(style, true);

    style = textpane.addStyle("Underline", null);
    StyleConstants.setUnderline(style, true);

    style = textpane.addStyle("Strike", null);
    StyleConstants.setStrikeThrough(style, true);

    boldb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Bold"),
                    false);
        }
    });

    italb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Italic"),
                    false);
        }

    });

    strib.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Strike"),
                    false);
        }

    });

    undeb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Underline"),
                    false);
        }
    });

    pane.getViewport().add(textpane);
    panel.add(pane);

    f.add(panel);

    f.setSize(new Dimension(380, 320));
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:Main.java

/** 
 * Affect a stylised text into an instance of JTextPane. 
 * @param textPane Instance of JTextPane to affect.
 * @param textArray Array of strings.//from w w  w. jav a 2s .co m
 * @param styleArray Array of styles.  Must match the textArray.
 */
public static void setText(JTextPane textPane, String[] textArray, String[] styleArray) {
    StyledDocument doc = textPane.getStyledDocument();
    try {
        doc.remove(0, doc.getLength()); // Erase all the previous text.
        for (int i = 0; i < textArray.length; i++) {
            int offset = doc.getLength();
            javax.swing.text.Style style = textPane.getStyle(styleArray[i]);
            doc.insertString(offset, textArray[i], style);
            doc.setParagraphAttributes(offset, textArray[i].length(), style, true);
        }
        textPane.setCaretPosition(0);
    } catch (BadLocationException ignore) {
        ignore.printStackTrace();
    }
}