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(StyleContext styles) 

Source Link

Document

Constructs a styled document with the default content storage implementation and a shared set of styles.

Usage

From source file:StylesExample8.java

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

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

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

    // Build the styles
    createDocumentStyles(sc);

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

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

From source file:TextPaneElements.java

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

    JFrame f = new JFrame("Text Pane Elements");

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

    // Build the styles
    createDocumentStyles(sc);

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);

                // Dump the element structure
                ((AbstractDocument) pane.getDocument()).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(500, 300);
    f.setVisible(true);
}

From source file:ExtendedParagraphExample.java

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

    JFrame f = new JFrame("Extended Paragraph Example");

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

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Build the styles
                createDocumentStyles(sc);

                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);
            }
        });
    } 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:ome.formats.importer.gui.GuiCommonElements.java

/**
 * Add a text pane to the parent container
 * /*from w  ww  .  j a va2s  .c  om*/
 * @param container - parent container
 * @param text - text for new Text Pane
 * @param placement - TableLayout placement within the parent container
 * @param debug - turn on/off red debug borders
 * @return new JTextPane
 */
public static synchronized JTextPane addTextPane(Container container, String text, String placement,
        boolean debug) {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);

    try {
        document.insertString(document.getLength(), text, null);
    } catch (BadLocationException e) {
        log.error("BadLocationException inserting text to document.");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    container.add(textPane, placement);

    if (debug == true)
        textPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red),
                textPane.getBorder()));

    return textPane;
}

From source file:ome.formats.importer.gui.GuiCommonElements.java

/**
 * A version of addTextPane that lets you add a style and context
 * /*from  w  w w. j  av a 2s . c  o  m*/
 * @param container - parent container
 * @param text - text for new Text Pane
 * @param placement - TableLayout placement within the parent container
 * @param context - context for new text pane
 * @param style - style to apply to new text pane
 * @param debug - turn on/off red debug borders
 * @return new JTextPane
 */
public static synchronized JTextPane addTextPane(Container container, String text, String placement,
        StyleContext context, Style style, boolean debug) {
    StyledDocument document = new DefaultStyledDocument(context);

    try {
        document.insertString(document.getLength(), text, style);
    } catch (BadLocationException e) {
        log.error("BadLocationException inserting text to document.");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    container.add(textPane, placement);

    if (debug == true)
        textPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red),
                textPane.getBorder()));

    return textPane;
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Formats the text and displays it in a {@link JTextPane}.
 * // www  .j  av a2 s.  c o m
 * @param text          The text to display.
 * @param foreground   The foreground color.
 * @return See above.
 */
public static JTextPane buildTextPane(String text, Color foreground) {
    if (text == null)
        text = "";
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
    if (foreground != null)
        StyleConstants.setForeground(style, foreground);
    try {
        document.insertString(document.getLength(), text, style);
    } catch (BadLocationException e) {
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);
    textPane.setEditable(false);
    textPane.setFocusable(false);
    return textPane;
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/** Builds the UI component displaying the exception.*/
public static JTextPane buildExceptionArea() {
    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    JTextPane textPane = new JTextPane(document);
    textPane.setOpaque(false);/*from  ww w .  j  a  v a 2  s . c om*/
    textPane.setEditable(false);

    // Create one of each type of tab stop
    List<TabStop> list = new ArrayList<TabStop>();

    // Create a left-aligned tab stop at 100 pixels from the left margin
    float pos = 15;
    int align = TabStop.ALIGN_LEFT;
    int leader = TabStop.LEAD_NONE;
    TabStop tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a right-aligned tab stop at 200 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_RIGHT;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a center-aligned tab stop at 300 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_CENTER;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a decimal-aligned tab stop at 400 pixels from the left margin
    pos = 15;
    align = TabStop.ALIGN_DECIMAL;
    leader = TabStop.LEAD_NONE;
    tstop = new TabStop(pos, align, leader);
    list.add(tstop);

    // Create a tab set from the tab stops
    TabSet tabs = new TabSet(list.toArray(new TabStop[0]));

    // Add the tab set to the logical style;
    // the logical style is inherited by all paragraphs
    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabs);
    textPane.setLogicalStyle(style);
    Style debugStyle = document.addStyle("StyleName", null);
    StyleConstants.setForeground(debugStyle, Color.BLACK);
    StyleConstants.setFontFamily(debugStyle, "SansSerif");
    StyleConstants.setFontSize(debugStyle, 12);
    StyleConstants.setBold(debugStyle, false);
    return textPane;
}