Example usage for javax.swing.text StyleConstants setFontFamily

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

Introduction

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

Prototype

public static void setFontFamily(MutableAttributeSet a, String fam) 

Source Link

Document

Sets the font attribute.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();
    StyledDocument doc = (StyledDocument) textPane.getDocument();

    Style style = doc.addStyle("StyleName", null);

    StyleConstants.setFontFamily(style, "SansSerif");

    // Append to document
    doc.insertString(doc.getLength(), "Some Text", style);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String s = new Date().toString();
    JTextPane jtp = new JTextPane();
    StyledDocument doc = (StyledDocument) jtp.getDocument();

    SimpleAttributeSet normal = new SimpleAttributeSet();
    StyleConstants.setFontFamily(normal, "SansSerif");
    StyleConstants.setFontSize(normal, 16);

    SimpleAttributeSet boldBlue = new SimpleAttributeSet(normal);
    StyleConstants.setBold(boldBlue, true);
    StyleConstants.setForeground(boldBlue, Color.blue);

    SimpleAttributeSet highAlert = new SimpleAttributeSet(boldBlue);
    StyleConstants.setFontSize(highAlert, 18);
    StyleConstants.setItalic(highAlert, true);
    StyleConstants.setForeground(highAlert, Color.red);

    doc.insertString(doc.getLength(), s + "\n", normal);
    doc.insertString(doc.getLength(), s + "\n", boldBlue);
    doc.insertString(doc.getLength(), s + "\n", highAlert);
    f.add(jtp);//from   www .j  a v  a2 s. c om
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int cp = 0;//from   w w w .  j a v  a 2 s .c  o m
    StyledDocument doc;
    JTextPane jta = new JTextPane();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    doc = jta.getStyledDocument();
    JScrollPane jsp = new JScrollPane(jta);
    jsp.setPreferredSize(new Dimension(400, 400));
    String[] fnt = ge.getAvailableFontFamilyNames();
    MutableAttributeSet mas = jta.getInputAttributes();
    for (int i = 0; i < fnt.length; i++) {
        StyleConstants.setBold(mas, false);
        StyleConstants.setItalic(mas, false);
        StyleConstants.setFontFamily(mas, fnt[i]);
        StyleConstants.setFontSize(mas, 16);

        doc.insertString(cp, fnt[i] + "\n", mas);

        StyleConstants.setBold(mas, true);
        doc.insertString(cp, fnt[i] + "bold \n", mas);
        StyleConstants.setItalic(mas, true);

        doc.insertString(cp, fnt[i] + "bold and italic\n", mas);
        StyleConstants.setBold(mas, false);
        doc.insertString(cp, fnt[i] + "italic\n", mas);
    }
    JFrame frm = new JFrame();
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setLayout(new BorderLayout());
    frm.add(jsp, BorderLayout.CENTER);
    frm.setLocation(100, 100);
    frm.pack();
    frm.setVisible(true);
}

From source file:StylesExample4.java

public static void main(String[] args) {
    try {//from w  ww .  j  a v  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:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(new JButton(new AbstractAction("Update") {
        @Override//from  w  w  w  . ja v a2  s .com
        public void actionPerformed(ActionEvent ae) {

            StyledDocument doc = (StyledDocument) textPane.getDocument();
            SimpleAttributeSet style = new SimpleAttributeSet();
            StyleConstants.setFontFamily(style, "Serif");
            StyleConstants.setFontSize(style, size++);
            try {
                doc.insertString(doc.getLength(), " one two three", style);
                Dimension d = textPane.getPreferredSize();
                Rectangle r = textPane.modelToView(textPane.getDocument().getLength());
                d.height = r.y + r.height;
                textPane.setPreferredSize(d);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
            frame.pack();
        }

    }));
    frame.add(buttonPanel, BorderLayout.NORTH);
    textPane.setText("this is a test.");
    textPane.setBorder(new LineBorder(Color.BLACK));

    frame.add(new JScrollPane(textPane));
    frame.pack();
    frame.setVisible(true);
}

From source file:StylesExample2.java

public static void main(String[] args) {
    try {/*from  w w w. ja  va 2s. c  om*/
        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:StylesExample7.java

public static void createDocumentStyles(StyleContext sc) {
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);

    // Create and add the main document style
    Style mainStyle = sc.addStyle(mainStyleName, 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
    Style cwStyle = sc.addStyle(charStyleName, null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.green);

    // Create and add the heading style
    Style heading2Style = sc.addStyle(heading2StyleName, 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);
}

From source file:StylesExample8.java

public static void createDocumentStyles(StyleContext sc) {
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);

    // Create and add the main document style
    Style mainStyle = sc.addStyle(mainStyleName, 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
    Style cwStyle = sc.addStyle(charStyleName, null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.green);

    // Create and add the heading style
    Style heading2Style = sc.addStyle(heading2StyleName, 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);

    // Create and add the Component style
    Class thisClass = StylesExample8.class;
    URL url = thisClass.getResource("java2s.gif");
    ImageIcon icon = new ImageIcon(url);
    JLabel comp = new JLabel("Displaying text with attributes", icon, JLabel.CENTER);
    comp.setVerticalTextPosition(JLabel.BOTTOM);
    comp.setHorizontalTextPosition(JLabel.CENTER);
    comp.setFont(new Font("serif", Font.BOLD | Font.ITALIC, 14));
    Style componentStyle = sc.addStyle(componentStyleName, null);
    StyleConstants.setComponent(componentStyle, comp);

    // The paragraph style for the component
    Style compParagraphStyle = sc.addStyle(compParaName, null);
    StyleConstants.setSpaceAbove(compParagraphStyle, (float) 16.0);
}

From source file:TextPaneElements.java

public static void createDocumentStyles(StyleContext sc) {
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);

    // Create and add the main document style
    Style mainStyle = sc.addStyle(mainStyleName, 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
    Style cwStyle = sc.addStyle(charStyleName, null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.green);

    // Create and add the heading style
    Style heading2Style = sc.addStyle(heading2StyleName, 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);

    // Create and add the Component style
    Class thisClass = TextPaneElements.class;
    URL url = thisClass.getResource("java2s.gif");
    ImageIcon icon = new ImageIcon(url);
    JLabel comp = new JLabel("Displaying text with attributes", icon, JLabel.CENTER);
    comp.setVerticalTextPosition(JLabel.BOTTOM);
    comp.setHorizontalTextPosition(JLabel.CENTER);
    comp.setFont(new Font("serif", Font.BOLD | Font.ITALIC, 14));
    Style componentStyle = sc.addStyle(componentStyleName, null);
    StyleConstants.setComponent(componentStyle, comp);

    // The paragraph style for the component
    Style compParagraphStyle = sc.addStyle(compParaName, null);
    StyleConstants.setSpaceAbove(compParagraphStyle, (float) 16.0);
}

From source file:ExtendedParagraphExample.java

public static void createDocumentStyles(StyleContext sc) {
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);

    // Create and add the main document style
    Style mainStyle = sc.addStyle(mainStyleName, 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
    Style cwStyle = sc.addStyle(charStyleName, null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.white);

    // Create and add the heading style
    Style heading2Style = sc.addStyle(heading2StyleName, 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);

    // Create and add the extended para styles
    Style paraStyle = sc.addStyle(paraStyleName, null);
    Color bgColor = Color.gray;
    ExtendedStyleConstants.setParagraphBackground(paraStyle, bgColor);
    ExtendedStyleConstants.setParagraphBorder(paraStyle,
            BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2),
                    BorderFactory.createCompoundBorder(
                            BorderFactory.createEtchedBorder(bgColor.brighter(), bgColor.darker()),
                            BorderFactory.createEmptyBorder(4, 4, 4, 4))));
}