Insert a line into the document with the specified font

I know that I can set the font family in an AttributeSet as follows:

        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setFontFamily(set, "Monospace");

        doc.insertString(
            caretPosition, text, set);

But I really want to set the font:

        StyleConstants.setFont(set, "Courier New");

However, there is no StyleConstants.setFont () method.

So how do I set the font in an AttributeSet? (Note that I'm free to use an AttributeSet implementation other than SimpleAttributeSet. I just used this one.)

(Note that my real goal is to insert a line into the document using the specified font.)

+5
source share
2 answers

You can set all font attributes using StyleConstants:

SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setFontFamily(set, "Monospace");
StyleConstants.setFontSize(set, 22);
StyleConstants.setBold(set, true);
StyleConstants.setItalic(set, true);
+2
source

In my case

SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setFontFamily(set, "Monospace");

. "" "":

StyleConstants.setFontFamily(set, "Monospaced");

, :

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fnt = ge.getAvailableFontFamilyNames();

for (String f : fnt){
            System.out.println(f);
}

+3

All Articles