Example usage for javax.swing.text StyleConstants FontFamily

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

Introduction

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

Prototype

Object FontFamily

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

Click Source Link

Document

Name of the font family.

Usage

From source file:Main.java

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

    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
    aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");

    JTextPane tPane1 = new JTextPane();
    tPane1.setCharacterAttributes(aset, false);

    f.add(tPane1, BorderLayout.CENTER);

    f.pack();/*from  ww  w .ja v  a 2  s  . c  om*/
    f.setVisible(true);
}

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  ww w  .j av  a2  s  .  co  m*/
        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  www  . j a  v  a2s. co 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: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 w  ww  .  j av  a2  s.co m*/
    tp.setCharacterAttributes(aset, false);
    tp.replaceSelection(msg);
}

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

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  .ja va2s  . c  om*/

    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:tk.tomby.tedit.editorkits.HighlightPlainContext.java

/**
 * Creates a new HighlightContext object.
 *
 * @param syntax DOCUMENT ME!/*from   ww w .jav  a2 s  .co m*/
 */
public HighlightPlainContext(Syntax syntax) {
    super();

    tokenizer = new Tokenizer(syntax);

    Style style = addStyle(Syntax.UNKNOWN_NAME, getStyle(DEFAULT_STYLE));

    int red = PreferenceManager.getInt("general.editor.foreground.red", 0);
    int green = PreferenceManager.getInt("general.editor.foreground.green", 0);
    int blue = PreferenceManager.getInt("general.editor.foreground.blue", 0);
    style.addAttribute(StyleConstants.Foreground, new Color(red, green, blue));

    String font = PreferenceManager.getString("general.editor.font", "Monospaced");
    int size = PreferenceManager.getInt("general.editor.fontSize", 12);
    style.addAttribute(StyleConstants.FontFamily, new Font(font, Font.PLAIN, size));

    createStyle(Syntax.KEYWORD1_NAME, "highlight.keyword1");
    createStyle(Syntax.KEYWORD2_NAME, "highlight.keyword2");
    createStyle(Syntax.KEYWORD3_NAME, "highlight.keyword3");
    createStyle(Syntax.KEYWORD4_NAME, "highlight.keyword4");

    createStyle(Syntax.COMMENT1_NAME, "highlight.comment1");
    createStyle(Syntax.COMMENT2_NAME, "highlight.comment2");
    createStyle(Syntax.COMMENT3_NAME, "highlight.comment3");
    createStyle(Syntax.COMMENT4_NAME, "highlight.comment3");

    createStyle(Syntax.LITERAL1_NAME, "highlight.literal1");
    createStyle(Syntax.LITERAL2_NAME, "highlight.literal2");
    createStyle(Syntax.LITERAL3_NAME, "highlight.literal3");
    createStyle(Syntax.LITERAL4_NAME, "highlight.literal4");

    createStyle(Syntax.IDENTIFIER1_NAME, "highlight.identifier1");
    createStyle(Syntax.IDENTIFIER2_NAME, "highlight.identifier2");
    createStyle(Syntax.IDENTIFIER3_NAME, "highlight.identifier3");
    createStyle(Syntax.IDENTIFIER4_NAME, "highlight.identifier4");

    createStyle(Syntax.OPERATOR_NAME, "highlight.operator");
    createStyle(Syntax.SEPARATOR_NAME, "highlight.separator");
    createStyle(Syntax.LABEL_NAME, "highlight.label");
    createStyle(Syntax.MARKUP_NAME, "highlight.markup");
    createStyle(Syntax.DIGIT_NAME, "highlight.digit");
    createStyle(Syntax.SCRIPTLET_NAME, "highlight.scriptlet");

    MessageManager.addMessageListener(MessageManager.PREFERENCE_GROUP_NAME, this);
}

From source file:tk.tomby.tedit.editorkits.HighlightPlainContext.java

/**
 * DOCUMENT ME!//from ww  w .j  a va 2s .  c om
 *
 * @param message DOCUMENT ME!
 */
public void receiveMessage(PreferenceMessage message) {
    if (message.getKey().startsWith("highlight")) {
        int index = message.getKey().indexOf('.');
        String styleName = message.getKey().substring(index + 1);

        log.debug("styleName=" + styleName);

        HighlightModel model = (HighlightModel) message.getNewValue();

        Style style = getStyle(styleName);
        style.addAttribute(StyleConstants.Foreground, model.getColor());
        style.addAttribute(StyleConstants.FontFamily, model.getFont());
    }
}

From source file:tk.tomby.tedit.editorkits.HighlightPlainContext.java

/**
 * DOCUMENT ME!//from  w  ww .  j ava2 s.co m
 *
 * @param name DOCUMENT ME!
 * @param key DOCUMENT ME!
 */
private void createStyle(String name, String key) {
    HighlightModel model = new HighlightModel(key);

    Style style = addStyle(name, getStyle(DEFAULT_STYLE));
    style.addAttribute(StyleConstants.Foreground, model.getColor());
    style.addAttribute(StyleConstants.FontFamily, model.getFont());
}