Example usage for javax.swing.text DefaultStyledDocument insertString

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

Introduction

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

Prototype

public void insertString(int offs, String str, AttributeSet a) throws BadLocationException 

Source Link

Document

Inserts some content into the document.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    final Style heading2Style = sc.addStyle("Heading2", null);
    heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
    heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
    heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
    heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

    try {/*from  w w  w.j  av a 2s .c om*/
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    doc.insertString(0, text, null);

                    doc.setParagraphAttributes(0, 1, heading2Style, false);
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample5.java

public static void main(String[] args) {
    try {/*from   ww w. j  a va2  s .c  o m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 5");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Create and add the style
    final Style heading2Style = sc.addStyle("Heading2", null);
    heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
    heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
    heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
    heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Finally, apply the style to the heading
                    doc.setParagraphAttributes(0, 1, heading2Style, false);

                    // Set the foreground and font
                    pane.setForeground(Color.blue);
                    pane.setFont(new Font("serif", Font.PLAIN, 12));
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample4.java

public static void main(String[] args) {
    try {/*from   www.  jav a  2  s .co m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 4");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    // Create and add the main document style
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    final Style mainStyle = sc.addStyle("MainStyle", defaultStyle);
    StyleConstants.setLeftIndent(mainStyle, 16);
    StyleConstants.setRightIndent(mainStyle, 16);
    StyleConstants.setFirstLineIndent(mainStyle, 16);
    StyleConstants.setFontFamily(mainStyle, "serif");
    StyleConstants.setFontSize(mainStyle, 12);

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Set the logical style
                    doc.setLogicalStyle(0, mainStyle);
                } catch (BadLocationException e) {
                }
                doc.dump(System.out);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample2.java

public static void main(String[] args) {
    try {// w w  w  .  j  a  v a  2s.  c o m
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 2");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    // Create and add the main document style
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    final Style mainStyle = sc.addStyle("MainStyle", defaultStyle);
    StyleConstants.setLeftIndent(mainStyle, 16);
    StyleConstants.setRightIndent(mainStyle, 16);
    StyleConstants.setFirstLineIndent(mainStyle, 16);
    StyleConstants.setFontFamily(mainStyle, "serif");
    StyleConstants.setFontSize(mainStyle, 12);

    // Create and add the constant width style
    final Style cwStyle = sc.addStyle("ConstantWidth", null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.green);

    // Create and add the heading style
    final Style heading2Style = sc.addStyle("Heading2", null);
    StyleConstants.setForeground(heading2Style, Color.red);
    StyleConstants.setFontSize(heading2Style, 16);
    StyleConstants.setFontFamily(heading2Style, "serif");
    StyleConstants.setBold(heading2Style, true);
    StyleConstants.setLeftIndent(heading2Style, 8);
    StyleConstants.setFirstLineIndent(heading2Style, 0);

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Set the logical style
                    doc.setLogicalStyle(0, mainStyle);

                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Apply the character attributes
                    doc.setCharacterAttributes(49, 13, cwStyle, false);
                    doc.setCharacterAttributes(223, 14, cwStyle, false);
                    doc.setCharacterAttributes(249, 14, cwStyle, false);
                    doc.setCharacterAttributes(286, 8, cwStyle, false);
                    doc.setCharacterAttributes(475, 14, cwStyle, false);
                    doc.setCharacterAttributes(497, 21, cwStyle, false);
                    doc.setCharacterAttributes(557, 9, cwStyle, false);
                    doc.setCharacterAttributes(639, 12, cwStyle, false);
                    doc.setCharacterAttributes(733, 21, cwStyle, false);
                    doc.setCharacterAttributes(759, 9, cwStyle, false);

                    // Finally, apply the style to the heading
                    doc.setParagraphAttributes(0, 1, heading2Style, false);
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java

public void addField() {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) availableFieldsComp.getTree()
            .getLastSelectedPathComponent();
    if (node == null || !node.isLeaf() || !(node.getUserObject() instanceof DataObjDataFieldWrapper)) {
        return; // not really a field that can be added, just empty or a string
    }/*from w  ww .  j ava  2  s  .co  m*/

    Object obj = node.getUserObject();
    if (obj instanceof DataObjDataFieldWrapper) {
        DataObjDataFieldWrapper wrapper = (DataObjDataFieldWrapper) obj;
        String sep = sepText.getText();
        if (StringUtils.isNotEmpty(sep)) {
            try {
                DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument();
                if (doc.getLength() > 0) {
                    doc.insertString(doc.getLength(), sep, null);
                }
            } catch (BadLocationException ble) {
            }

        }
        insertFieldIntoTextEditor(wrapper);
        setHasChanged(true);
    }
}

From source file:org.af.gMCP.gui.AboutDialog.java

private DefaultStyledDocument getDocument() {
    DefaultStyledDocument doc = new DefaultStyledDocument();
    logger.info("Creating About-Text.");
    try {/*from   www  .  j  a v  a 2  s  .  c o m*/
        doc.insertString(doc.getLength(),
                "gMCP " + Configuration.getInstance().getGeneralConfig().getVersionNumber() + "\n\n", getH1());
        doc.insertString(doc.getLength(),
                "by Kornelius Rohmeyer and Florian Klinglmueller is distributed under GPL 2.0." + "\n\n",
                getT());
        doc.insertString(doc.getLength(),
                "This program uses the libraries log4j, JLaTeXMath, POI, iText (2.1.4), jxlayer,\n swingworker, commons logging/lang, JRI and JGoodies Forms.\n",
                getT());
        doc.insertString(doc.getLength(),
                "\n" + "This program is free software; you can redistribute it and/or\n"
                        + "modify it under the terms of the GNU General Public License\n"
                        + "as published by the Free Software Foundation; either version 2\n"
                        + "of the License, or (at your option) any later version.\n" + "\n"
                        + "This program is distributed in the hope that it will be useful,\n"
                        + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
                        + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
                        + "GNU General Public License for more details. It is included\n"
                        + "in the R distribution (in directory share/licenses) or can be\n"
                        + "found at: http://www.gnu.org/licenses/\n",
                getT());
        doc.setParagraphAttributes(0, doc.getLength(), getC(), true);
    } catch (BadLocationException ble) {
        logger.error("BadLocationException was thrown. Should never happen.", ble);
    }
    return doc;
}

From source file:org.af.gMCP.gui.dialogs.TellAboutOnlineUpate.java

private DefaultStyledDocument getDocument() {
    DefaultStyledDocument doc = new DefaultStyledDocument();
    logger.info("Creating About-Text.");
    try {//www.j  a v  a  2s  . co m
        doc.insertString(doc.getLength(),
                "The gMCP-GUI would like to check online for updates on each start-up.\n", getH1());
        doc.insertString(doc.getLength(),
                "No information about your computer is send.\n"
                        + "Nevertheless you can disable this feature with the following checkbox\n"
                        + "or later from the options dialog.",
                getT());
        doc.setParagraphAttributes(0, doc.getLength(), getC(), true);
    } catch (BadLocationException ble) {
        logger.error("BadLocationException was thrown. Should never happen.", ble);
    }
    return doc;
}

From source file:org.mutoss.gui.dialogs.AboutDialog.java

private DefaultStyledDocument getDocument() {
    DefaultStyledDocument doc = new DefaultStyledDocument();
    logger.info("Creating About-Text.");
    try {//from ww  w  .j  a v a2 s  .c  o m
        doc.insertString(doc.getLength(),
                "Crossover " + Configuration.getInstance().getGeneralConfig().getVersionNumber() + "\n\n",
                getH1());
        doc.insertString(doc.getLength(), "by Kornelius Rohmeyer is distributed under GPL 2.0.\n\n", getT());
        doc.insertString(doc.getLength(), "This program uses the libraries log4j, jxlayer,\n "
                + "swingworker, commons logging/lang, JRI and JGoodies Forms.\n", getT());
        doc.insertString(doc.getLength(),
                "\n" + "This program is free software; you can redistribute it and/or\n"
                        + "modify it under the terms of the GNU General Public License\n"
                        + "as published by the Free Software Foundation, Version 2.\n" + "\n"
                        + "This program is distributed in the hope that it will be useful,\n"
                        + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
                        + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
                        + "GNU General Public License for more details. It is included\n"
                        + "in the R distribution (in directory share/licenses) or can be\n"
                        + "found at: http://www.gnu.org/licenses/\n",
                getT());
        doc.setParagraphAttributes(0, doc.getLength(), getC(), true);
    } catch (BadLocationException ble) {
        logger.error("BadLocationException was thrown. Should never happen.", ble);
    }
    return doc;
}