JDK-4126975 : StyledDocument/JTextPane ignore FontFamily attribute
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6
  • CPU: x86
  • Submitted: 1998-04-08
  • Updated: 1998-05-16
  • Resolved: 1998-05-16
Related Reports
Duplicate :  
Description

Name: rm29839			Date: 04/08/98


The following test case attempts to display
each font family name within a JTextPane
using the font itself.
But as you will see, the font remains the
same. The "addAttribute" method of
StyleContext doesn't appear to recognize
"StyleConstants.FontFamily". Or am I doing
something wrong?
Here is the testcase:
---------------------------------------
import java.awt.Toolkit;
import java.awt.swing.text.AttributeSet;
import java.awt.swing.text.StyleConstants;
import java.awt.swing.text.StyleContext;
import java.awt.swing.text.DefaultStyledDocument;
import java.awt.swing.text.StyledDocument;
import java.awt.swing.JTextPane;
import java.awt.swing.JScrollPane;
import java.awt.swing.JFrame;

class FontTest {
    public static void main(String args[]){
        String fonts[] = Toolkit.getDefaultToolkit().getFontList();
        StyledDocument doc = new DefaultStyledDocument();
        JTextPane pane = new JTextPane(doc);
        JScrollPane scroller = new JScrollPane(pane);
        JFrame f = new JFrame();
        f.getContentPane().add(scroller);
        f.pack();
        f.setSize(300,600);
        f.show();

        for (int i =0; i < fonts.length; i++){
            String font= fonts[i];
            append(doc,font);
            scroller.validate();
            }
        }

    static void append(StyledDocument doc, String font)
        {
        int loc = doc.getLength();
        StyleContext s = StyleContext.getDefaultStyleContext();
        AttributeSet def = doc.getDefaultRootElement().getAttributes();
        AttributeSet attr = s.addAttribute(
                                def,
                                StyleConstants.FontFamily,font);
        try{
            doc.insertString(loc,font+"\n",attr);
            }
        catch(Exception x){}
        }
    }
(Review ID: 27176)
======================================================================