Example usage for javax.swing JTextPane replaceSelection

List of usage examples for javax.swing JTextPane replaceSelection

Introduction

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

Prototype

@Override
public void replaceSelection(String content) 

Source Link

Document

Replaces the currently selected content with new content represented by the given string.

Usage

From source file:PaneInsertionMethods.java

public static void main(String[] args) {

    final JTextPane pane = new JTextPane();

    pane.replaceSelection("text");
    pane.insertIcon(new ImageIcon("imageName.gif"));
    pane.insertComponent(new JButton("Click Me"));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.setSize(360, 180);//from w  w w .  jav  a  2s  . c  o m
    frame.setVisible(true);
}

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 ww  w.  j  a v  a  2 s .  co  m
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);
}

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  w w  w.  ja  v a 2  s.c om*/
            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);/* www.  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);
        }

        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);/*from  w  ww .  j  a  v a2s .  c  om*/
    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 . ja  v a 2 s .  co m
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);

}