1

I am trying to load a font to a label/button text on a swing app written in java 1.4. For some reason, the font loads without exception, but displays a completely diff font on the button. When I run the same code on 1.5 jre, it appears to work ok. Why does this happen?

UPDATE Printing a sysout on 1.4, 1.5 shows this:

jre 1.5 : java.awt.Font[family=Futura LT,name=Futura LT Medium,style=bold,size=14]

jre 1.4 : java.awt.Font[family=dialog,name=Futura LT Medium,style=bold,size=14]

The font name family is different! Why is that?

Below is an image to how it looks on diff jres (left is 1.5, right is 1.4)

enter image description here

Code

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JButton;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout.Node;



/**
 * 
 * @author Hans Muller (Hans.Muller@Sun.COM)
 */
public class Example2 extends Example {
    protected void initialize(String[] ignore) {
        super.initialize(ignore);

        String layoutDef = "(COLUMN (LEAF name=column1 weight=0.25) (LEAF name=column2 weight=0.25) (LEAF name=column3 weight=0.25) (LEAF name=column4 weight=0.25) )";
        Node modelRoot = MultiSplitLayout.parseModel(layoutDef);

        MultiSplitPane multiSplitPane = new MultiSplitPane();
        multiSplitPane.setDividerSize(5);
        multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
        JButton comp = new JButton("TEST TEXT");
        comp.setFont(loadFont("FuturaLT.ttf",14,Font.BOLD));
        multiSplitPane.add(comp, "column1");
        multiSplitPane.add(new JButton("Test Text"), "column2");
        Container cp = mainFrame.getContentPane();
        cp.add(multiSplitPane, BorderLayout.CENTER);
    }

    private static Font loadFont(String fontName, float size, int style) {

        InputStream openStream = FontColorScheme.class
                .getResourceAsStream("resources/fonts/FuturaLT/"
                        + fontName);
        ;
        try {
            Font font = Font.createFont(Font.TRUETYPE_FONT, openStream);
            Font finalFont = font.deriveFont((float) size).deriveFont(style);
            System.out.println("Loading font " + fontName + " " + finalFont);
            return finalFont;
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (openStream != null) {
                try {
                    openStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void main(final String[] args) {
        System.setProperty("awt.useSystemAAFontSettings", "off");
        Runnable doCreateAndShowGUI = new Runnable() {
            public void run() {
                try {
                    Example2 app = new Example2();
                    app.initialize(args);
                    app.show();
                } catch (Exception e) {
                    // TBD log an error
                }
            }
        };
        SwingUtilities.invokeLater(doCreateAndShowGUI);
    }
}
Community
  • 1
  • 1
Jay
  • 2,394
  • 11
  • 54
  • 98
  • 2
    Looks like two different look and feels – MadProgrammer Jun 12 '13 at 07:34
  • 2
    As an aside, why are you trying to write code for obsolete JREs? I doubt many people would (still) care what fonts Java 1.5 or 1.4 use, *or* how they render.. – Andrew Thompson Jun 12 '13 at 07:47
  • @MadProgrammer the code is all that. doing nothing different for look and feels. – Jay Jun 12 '13 at 07:56
  • @AndrewThompson: Some organizations will not move to a new version of Java unless they are forced to move. It took us 6 months to move to Java 1.5 because of all the code changes and code testing we had to do because we used enum as a field name in a few hundred classes. We're currently at 1.6 with no plans to move to 1.7 until we are forced to move by Glassfish updates. – Gilbert Le Blanc Jun 12 '13 at 09:09
  • @GilbertLeBlanc *"Some organizations.."* Yes, yes - I know there are multiple reasons why the OP **might** need to code for an older JRE, but I want to know the ***actual*** reason in this case. – Andrew Thompson Jun 12 '13 at 09:11

2 Answers2

2

rendering is correct, there are three areas

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @Jay don't know if is possible to set Ocean theme in Java1.4.xxx (Metal L&F in =>Java5), every constants including L&F are in [UIManager Defaults](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) by camickr – mKorbel Jun 12 '13 at 08:08
1

This can have many reasons. To understand this better, you should print the font that Java is using:

 System.out.println(button.getFont().toString());

That should answer the question whether both JREs use the same default font. If they do, make sure the font is a TrueType font (TTF) and not a bitmap font; bitmap fonts don't scale well. While TTF does scale better, some fonts look bad at certain sizes. So a font might look well at 10 and 12 pixel but odd at 11.

To solve the issue, either set the font yourself, chose a different font if you already override the system default or try to tweak the font size.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820