Example usage for javax.swing.text DefaultStyledDocument DefaultStyledDocument

List of usage examples for javax.swing.text DefaultStyledDocument DefaultStyledDocument

Introduction

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

Prototype

public DefaultStyledDocument() 

Source Link

Document

Constructs a default styled document.

Usage

From source file:Main.java

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

    StyledDocument doc = new DefaultStyledDocument();
    JTextPane textPane = new JTextPane(doc);
    textPane.setText("this is a test.");

    Random random = new Random();
    for (int i = 0; i < textPane.getDocument().getLength(); i++) {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set,
                new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        StyleConstants.setFontSize(set, random.nextInt(12) + 12);
        StyleConstants.setBold(set, random.nextBoolean());
        StyleConstants.setItalic(set, random.nextBoolean());
        StyleConstants.setUnderline(set, random.nextBoolean());

        doc.setCharacterAttributes(i, 1, set, true);
    }/*from   w w w  .ja v  a2s  . co  m*/

    frame.add(new JScrollPane(textPane));
    frame.setSize(500, 400);
    frame.setVisible(true);
}

From source file:SimpleAttributeBoldItalic.java

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

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);

    try {/*from w w w  .ja  v  a2 s . c o m*/
        document.insertString(document.getLength(), "Bold, Italic", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    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

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

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.LIGHT_GRAY);

    try {/*from  www  .  j a  v  a 2 s.c om*/
        document.insertString(document.getLength(), " Bold, Italic and light gray color", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    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: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);/*  www  .  j  a  va  2s  . c o  m*/
    JScrollPane scrollPane = new JScrollPane(textPane);
    frame.add(scrollPane, BorderLayout.CENTER);

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

From source file:SimpleAttributeBoldItalicColor.java

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

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.LIGHT_GRAY);

    try {/*from   w w  w.jav a2s  .  c o m*/
        document.insertString(document.getLength(), " Bold, Italic and light gray color", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    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: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 ww . j  a  v  a 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:SimpleAttributeSample.java

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

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);

    // Insert content
    try {/*from  ww w.  j ava  2  s  .  c o  m*/
        document.insertString(document.getLength(), "Hello Java", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.lightGray);

    // Insert content
    try {
        document.insertString(document.getLength(), " - Good-bye Visual Basic", attributes);
    } 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: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 {/*from  w  w  w.j  a v  a 2 s  .c  o 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("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:StyledSample.java

public static void main(String args[]) {
    String BOLD_ITALIC = "BoldItalic";
    String GRAY_PLAIN = "Gray";
    JFrame frame = new JFrame("Simple Attributes");
    Container content = frame.getContentPane();

    StyledDocument document = new DefaultStyledDocument();

    Style style = (Style) document.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setBold(style, true);
    StyleConstants.setItalic(style, true);
    document.addStyle(BOLD_ITALIC, null);

    //    style = document.getStyle(StyleContext.DEFAULT_STYLE);
    //    StyleConstants.setBold(style, false);
    //    StyleConstants.setItalic(style, false);
    //    StyleConstants.setForeground(style, Color.lightGray);
    //    document.addStyle(GRAY_PLAIN, null);

    style = document.getStyle(BOLD_ITALIC);

    // Insert content
    try {/*from w w w.  j a v  a  2s. c om*/
        document.insertString(document.getLength(), "Hello Java\n", style);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    style = document.getStyle(GRAY_PLAIN);

    // Insert content
    try {
        document.insertString(document.getLength(), " - Good-bye Visual Basic\n", style);
    } 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:framework.retrieval.engine.index.create.impl.file.parse.RTFFileContentParser.java

public String getContent(RFileDocument document, String charsetName) {
    String content = "";
    InputStream fileInputStream = null;
    try {/*from   www.ja  v  a  2 s .co  m*/
        fileInputStream = new FileInputStream(document.getFile());
        DefaultStyledDocument styledDoc = new DefaultStyledDocument();
        RTFEditorKit rtfEditorKit = new RTFEditorKit();
        rtfEditorKit.read(fileInputStream, styledDoc, 0);
        content = styledDoc.getText(0, styledDoc.getLength());
    } catch (Exception e) {
        RetrievalUtil.errorLog(log, document.getFile().getAbsolutePath(), e);
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (Exception e) {
            RetrievalUtil.errorLog(log, e);
        }
    }
    return content;
}