Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6313541

Fonts loaded with createFont cannot be converted into FontUIResource

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P4
    • 6
    • 1.4.1, 5.0
    • client-libs
    • 2d
    • b51
    • x86
    • windows_xp

    Backports

      Description

        A DESCRIPTION OF THE REQUEST :
        The following is from the FontUIResource class:
        A subclass of java.awt.Font that implements UIResource. UI classes which set default font properties should use this class.

        This Font subclass which implements the UIResource marker interface allows implementers of the ComponentUI.installUI and ComponentUI.uninstallUI to determine if the font is a default for the Look and Feel or if it was installed by the application. This prevents default fonts for one Look and Feel from being used when the Look and Feel is changed in the application.

        FontUIResource (or any other subclass of Font that implements the UIResource) has to call an accessible constructor of the Font class. Neither of the available constructors is able to convert a font loaded from a file with the creatFont() method to be used to construct a new Font. The only way to get a new Font instance from a created font is with one of the deriveFont() methods. This means that the resulting Font created as a result of the new FontUIResource will not display the loaded font, but will display the nearest registered font.

        It looks like the font2DHandle instance variable needs to be copied from the original Font to the new Font. The constructors for the

        JUSTIFICATION :
        This means that a Look and Feel cannot simply include a font file in a .jar file with the other classes of the custom Look and Feel. The font needs to be registered with the system in order to be displayed as a component default.



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The FontUIResource(Font) method should return a FontUIResource that will display the original font if it was loaded with a creatFont() method call.

        ---------- BEGIN SOURCE ----------
        /**
         * This example requires that a True Type font file that is not registered with
        * the operating system is available in the same directory as the FontTestPanel
        * class. You can pick up the free Augie handwriting font from the following
        * location: http://www.speakeasy.org/~ecf/augie.zip
         *
         */
        public class FontTestPanel extends JPanel {

        private JLabel testLabel = null;
        private JButton fontButton = null;
        private JLabel badFontLabel = null;
        private JButton badButton = null;
        private Font labelFont;
        private FontUIResource badLabelFont;

        /**
        * This is the default constructor
        */
        public FontTestPanel() {
        super();
        //
        // replace fontFileName with the name of the font file being used.
        //
        InputStream fontStream = FontTestPanel.class.getResourceAsStream("fontFileName.ttf");
        if (fontStream != null) {
        Font newFont;
        try {
        newFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
        labelFont = newFont.deriveFont((float)24);
        badLabelFont = new FontUIResource(labelFont);
        } catch (FontFormatException e1) {
        e1.printStackTrace();
        } catch (IOException e1) {
        e1.printStackTrace();
        }
        }
        initialize();
        }

        /**
        * This method initializes this
        *
        * @return void
        */
        private void initialize() {
        badFontLabel = new JLabel();
        badFontLabel.setText("Only font size will change with a button press!");
        testLabel = new JLabel();
        testLabel.setText("The font and size will both change with a button press");
        this.setSize(300, 200);
        this.add(testLabel, null);
        this.add(getFontButton(), null);
        this.add(badFontLabel, null);
        this.add(getBadButton(), null);
        }

        /**
        * This method initializes fontButton
        *
        * @return javax.swing.JButton
        */
        private JButton getFontButton() {
        if (fontButton == null) {
        fontButton = new JButton();
        fontButton.setText("Change Font");
        fontButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
        testLabel.setFont(labelFont);
        testLabel.revalidate();
        }
        });
        }
        return fontButton;
        }

        /**
        * This method initializes badButton
        *
        * @return javax.swing.JButton
        */
        private JButton getBadButton() {
        if (badButton == null) {
        badButton = new JButton();
        badButton.setText("Change Font");
        badButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
        badFontLabel.setFont(badLabelFont);
        badFontLabel.revalidate();
        }
        });
        }
        return badButton;
        }

        }
        ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

                prr Philip Race
                prr Philip Race
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: