7

The native Windows LookAndFeel in Java 6 seems to incorrectly size some fonts.

Test program:

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class Test {
  public static void main(String[] arg) throws Exception {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
          e.printStackTrace();
        }

        final JMenuBar mb = new JMenuBar();

        final JMenu file = new JMenu("File");
        file.setMnemonic(KeyEvent.VK_F);
        mb.add(file);

        final JToolBar toolbar = new JToolBar();
        final JButton button = new JButton("Button");
        toolbar.add(button);

        final JLabel label = new JLabel("Basic Colors");

        final JPanel panel = new JPanel(new BorderLayout());
        panel.add(toolbar, BorderLayout.PAGE_START);
        panel.add(label, BorderLayout.CENTER);       

        final JFrame frame = new JFrame();
        frame.setJMenuBar(mb);
        frame.add(panel);

        frame.setTitle("Test"); 
        frame.setSize(400,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }
}

Output, compared with a native Windows app on Vista:

comparison

While the text in the test app menu bar is sized correctly, the rest of the text is smaller than in the native app next to it. Zoomed in, you can see that the text in the test app JLabel is 1px shorter than in the native app:

zoomed in

Is this a bug in the Windows LaF, or are we misusing it? If it's a bug, is there a known workaround?

2
  • To me it looks as if the Swing L&F uses a different font.
    – user330315
    Oct 3, 2011 at 22:15
  • 1
    +1 Nicely prepared question. Oct 3, 2011 at 22:47

1 Answer 1

4

Java 6 uses its own font renderer, including the implementation of subpixel antialiasing / hinting whatsit. While the output is meant to be similar to Windows' rendering, either the top of the B being at a pixel boundary, or it being rounded, or both, throws the Java renderer off. The Windows font renderer decides to place the top of the letter above the boundary, while the Java one places it below. The 'l' looks like it's at the same height in both samples, so it doesn't seem like the renderer gets the height of every letter wrong. Maybe try comparing with some letters where the top is a straight line, like a T or an E?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.