/** * Sets the font of the text pane. * * 2003-2007 Philip Isenhour, public domain, http://javatechniques.com/blog/setting-jtextpane-font-and-color/ * * @param font the font to use */ public void setTextFont(Font font) { // Start with the current input attributes for the JTextPane. This // should ensure that we do not wipe out any existing attributes // (such as alignment or other paragraph attributes) currently // set on the text area. MutableAttributeSet attrs = getInputAttributes(); // Set the font family, size, and style, based on properties of // the Font object. Note that JTextPane supports a number of // character attributes beyond those supported by the Font class. // For example, underline, strike-through, super- and sub-script. StyleConstants.setFontFamily(attrs, font.getFamily()); StyleConstants.setFontSize(attrs, font.getSize()); StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0); StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0); // Retrieve the pane's document object StyledDocument doc = getStyledDocument(); // Replace the style for the entire document. We exceed the length // of the document by 1 so that text entered at the end of the // document uses the attributes. doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false); }