Example usage for javax.swing.text StyleConstants Bold

List of usage examples for javax.swing.text StyleConstants Bold

Introduction

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

Prototype

Object Bold

To view the source code for javax.swing.text StyleConstants Bold.

Click Source Link

Document

Name of the bold attribute.

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 {/* w  w  w  .j  a v a2 s . 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 w w  w  . ja v a 2s  .  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:au.org.ala.delta.ui.rtf.RTFWriter.java

private void configureAttributeHandlers() {
    _attributeHandlers.put(StyleConstants.Bold,
            new BooleanAttributeHandler(StyleConstants.Bold, CharacterAttributeType.Bold.keyword()));
    _attributeHandlers.put(StyleConstants.Italic,
            new BooleanAttributeHandler(StyleConstants.Italic, CharacterAttributeType.Italics.keyword()));
    _attributeHandlers.put(StyleConstants.Underline,
            new BooleanAttributeHandler(StyleConstants.Underline, CharacterAttributeType.Underline.keyword()));
    _attributeHandlers.put(StyleConstants.Subscript,
            new BooleanAttributeHandler(StyleConstants.Subscript, CharacterAttributeType.Subscript.keyword(),
                    CharacterAttributeType.NoSuperscriptOrSubscript.keyword()));
    _attributeHandlers.put(StyleConstants.Superscript,
            new BooleanAttributeHandler(StyleConstants.Superscript,
                    CharacterAttributeType.Superscript.keyword(),
                    CharacterAttributeType.NoSuperscriptOrSubscript.keyword()));

    // _attributeHandlers.put(StyleConstants.FontSize, new FontSizeAttributeHandler(StyleConstants.FontSize, CharacterAttributeType.FontSize.keyword(), 11));
    // _attributeHandlers.put(StyleConstants.FontFamily, new FontFamilyAttributeHandler(StyleConstants.FontFamily, CharacterAttributeType.Font.keyword()));
}

From source file:net.sf.jasperreports.engine.util.JEditorPaneHtmlMarkupProcessor.java

@Override
protected Map<Attribute, Object> getAttributes(AttributeSet attrSet) {
    Map<Attribute, Object> attrMap = new HashMap<Attribute, Object>();
    if (attrSet.isDefined(StyleConstants.FontFamily)) {
        attrMap.put(TextAttribute.FAMILY, StyleConstants.getFontFamily(attrSet));
    }//from   w w w . j a v a 2s.  c o  m

    if (attrSet.isDefined(StyleConstants.Bold)) {
        attrMap.put(TextAttribute.WEIGHT,
                StyleConstants.isBold(attrSet) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR);
    }

    if (attrSet.isDefined(StyleConstants.Italic)) {
        attrMap.put(TextAttribute.POSTURE, StyleConstants.isItalic(attrSet) ? TextAttribute.POSTURE_OBLIQUE
                : TextAttribute.POSTURE_REGULAR);
    }

    if (attrSet.isDefined(StyleConstants.Underline)) {
        attrMap.put(TextAttribute.UNDERLINE,
                StyleConstants.isUnderline(attrSet) ? TextAttribute.UNDERLINE_ON : null);
    }

    if (attrSet.isDefined(StyleConstants.StrikeThrough)) {
        attrMap.put(TextAttribute.STRIKETHROUGH,
                StyleConstants.isStrikeThrough(attrSet) ? TextAttribute.STRIKETHROUGH_ON : null);
    }

    if (attrSet.isDefined(StyleConstants.FontSize)) {
        attrMap.put(TextAttribute.SIZE, StyleConstants.getFontSize(attrSet));
    }

    if (attrSet.isDefined(StyleConstants.Foreground)) {
        attrMap.put(TextAttribute.FOREGROUND, StyleConstants.getForeground(attrSet));
    }

    if (attrSet.isDefined(StyleConstants.Background)) {
        attrMap.put(TextAttribute.BACKGROUND, StyleConstants.getBackground(attrSet));
    }

    //FIXME: why StyleConstants.isSuperscript(attrSet) does return false
    if (attrSet.isDefined(StyleConstants.Superscript) && !StyleConstants.isSubscript(attrSet)) {
        attrMap.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
    }

    if (attrSet.isDefined(StyleConstants.Subscript) && StyleConstants.isSubscript(attrSet)) {
        attrMap.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
    }

    return attrMap;
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatWritePanel.java

/**
 * Enables the bold style//from w w  w.ja  va  2  s. c o  m
 * @param b TRUE enable - FALSE disable
 */
public void setBoldStyleEnable(boolean b) {
    StyledEditorKit editorKit = (StyledEditorKit) editorPane.getEditorKit();
    MutableAttributeSet attr = editorKit.getInputAttributes();

    if (b && !StyleConstants.isBold(attr)) {
        setStyleConstant(new HTMLEditorKit.BoldAction(), StyleConstants.Bold);
    }
}

From source file:org.alder.fotobuchconvert.scribus.RtfToScribusConverter.java

void output(XmlBuilder xml, DefaultStyledDocument doc, ScribusWriter scribus) {
    log.debug("Starting conversion of RTF data");
    if (log.isTraceEnabled())
        doc.dump(System.err);/* w w w  .j  ava  2  s  .  c o m*/

    try {
        Element section = doc.getDefaultRootElement();
        log.trace(section);
        assert section.getName().equals("section");

        final int nj = section.getElementCount();
        for (int j = 0; j < nj; j++) {
            Element paragraph = section.getElement(j);
            log.trace(paragraph);
            assert section.getName().equals("paragraph");

            // boolean firstInPara = true;
            AttributeSet attr = paragraph.getAttributes();
            Integer alignment = (Integer) attr.getAttribute(StyleConstants.Alignment);

            boolean elementsInThisLine = false;
            final int ni = paragraph.getElementCount();
            for (int i = 0; i < ni; i++) {
                Element content = paragraph.getElement(i);
                assert section.getName().equals("content");

                int start = content.getStartOffset();
                int end = content.getEndOffset();

                attr = content.getAttributes();
                Boolean italic = (Boolean) attr.getAttribute(StyleConstants.Italic);
                Boolean bold = (Boolean) attr.getAttribute(StyleConstants.Bold);
                Boolean underline = (Boolean) attr.getAttribute(StyleConstants.Underline);
                String family = (String) attr.getAttribute(StyleConstants.Family);
                Integer fontSize = (Integer) attr.getAttribute(StyleConstants.Size);
                Color color = (Color) attr.getAttribute(StyleConstants.ColorConstants.Foreground);

                String text = doc.getText(start, end - start);

                // if (firstInPara && text.trim().isEmpty() && family ==
                // null
                // && fontSize == null)
                // continue;
                // else
                // firstInPara = false;
                if (i == ni - 1 && text.trim().isEmpty() && text.length() < 3)
                    continue;
                elementsInThisLine = true;

                while (text.endsWith("\n") || text.endsWith("\r"))
                    text = text.substring(0, text.length() - 1);

                log.debug(italic + " " + bold + " " + underline + " " + family + " " + fontSize + " " + color
                        + "\t\"" + text + "\"");

                XmlBuilder el = xml.add(C.EL_ITEXT).set(C.CH, text);

                if (bold == Boolean.TRUE && italic == Boolean.TRUE)
                    el.set(C.FONT, family + " Bold Italic");
                else if (bold == Boolean.TRUE)
                    el.set(C.FONT, family + " Bold");
                else if (italic == Boolean.TRUE)
                    el.set(C.FONT, family + " Italic");
                else
                    el.set(C.FONT, family + " Regular");

                if (fontSize != null)
                    el.set(C.FONTSIZE, fontSize);

                if (color != null && color.equals(Color.BLACK) && scribus != null) {
                    String colname = scribus.colorManager.getColorName(color);
                    el.set(C.FCOLOR, colname);
                }
            }

            if (!elementsInThisLine && j == nj - 1)
                break; // don't convert last line if empty

            XmlBuilder el = xml.add(C.EL_PARA);
            if (alignment != null)
                switch (alignment) {
                case StyleConstants.ALIGN_LEFT:
                    el.set(C.ALIGN, 0);
                    break;
                case StyleConstants.ALIGN_CENTER:
                    el.set(C.ALIGN, 1);
                    break;
                case StyleConstants.ALIGN_RIGHT:
                    el.set(C.ALIGN, 2);
                    break;
                case StyleConstants.ALIGN_JUSTIFIED:
                    el.set(C.ALIGN, 3);
                    break;
                }
        }
    } catch (BadLocationException e) {
        throw new RuntimeException("This error should not occour", e);
    }

}