Example usage for javax.swing.text StyledDocument setParagraphAttributes

List of usage examples for javax.swing.text StyledDocument setParagraphAttributes

Introduction

In this page you can find the example usage for javax.swing.text StyledDocument setParagraphAttributes.

Prototype

public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace);

Source Link

Document

Sets paragraph attributes.

Usage

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:TabSample.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Tab Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    //TabStop.ALIGN_CENTER, TabStop.ALIGN_DECIMAL,TabStop.ALIGN_LEFT, TabStop.ALIGN_RIGHT
    //"\tBAR\n", "\tCENTER\n", "\t3.14159265\n", "\tLEFT\n", "\tRIGHT\n" };

    SimpleAttributeSet attributes = new SimpleAttributeSet();

    TabStop tabstop = new TabStop(150, TabStop.ALIGN_BAR, TabStop.LEAD_DOTS);
    int position = document.getLength();
    document.insertString(position, "TabStop.ALIGN_BAR", null);

    TabSet tabset = new TabSet(new TabStop[] { tabstop });
    StyleConstants.setTabSet(attributes, tabset);
    document.setParagraphAttributes(position, 1, attributes, false);

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);/*from  w w w. ja va 2  s. co m*/
    JScrollPane scrollPane = new JScrollPane(textPane);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TextTabSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Tab Attributes");
    Container content = frame.getContentPane();

    StyledDocument document = new DefaultStyledDocument();

    int positions[] = { TabStop.ALIGN_BAR, TabStop.ALIGN_CENTER, TabStop.ALIGN_DECIMAL, TabStop.ALIGN_LEFT,
            TabStop.ALIGN_RIGHT };
    String strings[] = { "\tBAR\n", "\tCENTER\n", "\t3.14159265\n", "\tLEFT\n", "\tRIGHT\n" };

    SimpleAttributeSet attributes = new SimpleAttributeSet();

    for (int i = 0, n = positions.length; i < n; i++) {
        TabStop tabstop = new TabStop(150, positions[i], TabStop.LEAD_DOTS);
        try {//  www  . j av a 2  s  .  c  om
            int position = document.getLength();
            document.insertString(position, strings[i], null);
            TabSet tabset = new TabSet(new TabStop[] { tabstop });
            StyleConstants.setTabSet(attributes, tabset);
            document.setParagraphAttributes(position, 1, attributes, false);
        } catch (BadLocationException badLocationException) {
            System.err.println("Oops");
        }
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);
    content.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Tab Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    int positions[] = { TabStop.ALIGN_BAR, TabStop.ALIGN_CENTER, TabStop.ALIGN_DECIMAL, TabStop.ALIGN_LEFT,
            TabStop.ALIGN_RIGHT };
    String strings[] = { "\tTabStop.ALIGN_BAR\n", "\tTabStop.ALIGN_CENTER\n", "\tTabStop.ALIGN_DECIMAL\n",
            "\tTabStop.ALIGN_LEFT\n", "\tTabStop.ALIGN_RIGHT\n" };

    SimpleAttributeSet attributes = new SimpleAttributeSet();

    for (int i = 0, n = positions.length; i < n; i++) {
        TabStop tabstop = new TabStop(150, positions[i], TabStop.LEAD_DOTS);
        try {/*from w w  w  .j  a  va  2  s. co m*/
            int position = document.getLength();
            document.insertString(position, strings[i], null);
            TabSet tabset = new TabSet(new TabStop[] { tabstop });
            StyleConstants.setTabSet(attributes, tabset);
            document.setParagraphAttributes(position, 1, attributes, false);
        } catch (BadLocationException badLocationException) {
            System.err.println("Bad Location");
        }
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.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./*  w ww.  j  av  a  2 s.c o  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();
    }
}

From source file:Main.java

public Main() throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(edit);/*w w  w.j  a va 2s.  c om*/
    edit.setEditorKit(new MyEditorKit());
    SimpleAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_CENTER);
    StyledDocument doc = (StyledDocument) edit.getDocument();
    doc.insertString(0, "111\n2222222\n3333", attrs);
    doc.setParagraphAttributes(0, doc.getLength() - 1, attrs, false);

    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:Main.java

public TextPaneAttributes() {
    JTextPane textPane = new JTextPane();
    StyledDocument doc = textPane.getStyledDocument();
    MutableAttributeSet standard = new SimpleAttributeSet();

    StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
    doc.setParagraphAttributes(0, 0, standard, true);
    MutableAttributeSet keyWord = new SimpleAttributeSet();

    StyleConstants.setForeground(keyWord, Color.red);
    StyleConstants.setItalic(keyWord, true);

    textPane.setText("this is a test. \nthis is a four.");

    doc.setCharacterAttributes(0, 3, keyWord, false);
    doc.setCharacterAttributes(19, 4, keyWord, false);
    try {// w ww.j  a  v a  2 s.  co m
        doc.insertString(0, "Start of text\n", null);
        doc.insertString(doc.getLength(), "End of text\n", keyWord);
    } catch (Exception e) {
    }
    MutableAttributeSet selWord = new SimpleAttributeSet();

    StyleConstants.setForeground(selWord, Color.RED);
    StyleConstants.setItalic(selWord, true);

    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(200, 200));
    add(scrollPane);

    JButton toggleButton = new JButton("Find 'four'");
    toggleButton.addActionListener(e -> {
        int index = textPane.getText().indexOf("four");
        StyledDocument doc1 = textPane.getStyledDocument();
        doc1.setCharacterAttributes(index, 4, selWord, false);
    });
    add(toggleButton, BorderLayout.SOUTH);
}

From source file:Tela.java

/**
 * Creates new form Tela/*  w  ww. j a  v  a2s  .  c  o m*/
 */
public Tela() {
    initComponents();
    jPanel2.setLayout(new BorderLayout());
    exibeGrafico(new ArrayList<Double>());

    this.setVisible(true);

    StyledDocument doc = txtArtigos.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    doc.setParagraphAttributes(0, doc.getLength(), center, false);
}

From source file:dylemator.DylematorUI.java

private void displayCenteredText(String text) {
    textArea.setText("");
    text = "\n\n\n" + text;
    SimpleAttributeSet attribs = new SimpleAttributeSet();
    if (sd == null)
        sd = new SettingsDialog(this, true);
    float fnt = sd.getFontSize();
    StyledDocument doc = (StyledDocument) textArea.getDocument();
    Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);//doc.addStyle("MyStyle",null);
    StyleConstants.setFontSize(style, (int) fnt);
    StyleConstants.setLineSpacing(attribs, 0.5f);
    StyleConstants.setFontFamily(style, "Segoe UI");
    StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_CENTER);
    textArea.setParagraphAttributes(attribs, true);

    try {//from   w w  w.ja v  a2s. co m
        doc.insertString(doc.getLength(), text, style);
        doc.setParagraphAttributes(0, doc.getLength() - 1, attribs, false);
    } catch (BadLocationException ex) {
        Logger.getLogger(DylematorUI.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.optaplanner.benchmark.impl.aggregator.swingui.BenchmarkAggregatorFrame.java

private JComponent createNoPlannerFoundTextField() {
    String infoMessage = "No planner benchmarks have been found in the benchmarkDirectory ("
            + benchmarkAggregator.getBenchmarkDirectory() + ").";
    JTextPane textPane = new JTextPane();

    textPane.setEditable(false);//www .j  a v a2s.  c om
    textPane.setText(infoMessage);

    // center info message
    StyledDocument styledDocument = textPane.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    StyleConstants.setBold(center, true);
    styledDocument.setParagraphAttributes(0, styledDocument.getLength(), center, false);
    return textPane;
}