Font Faces and Styles Not Set

Discussion in 'Java' started by Mike Westerfield, May 14, 2004.

  1. I'd like to see if anyone can offer an explanation for some font
    issues before I report the issues as bugs to Sun and Apple. Please let
    me know if you can explain the following as correct behavior.

    It appears to me that a lot of fonts simply don't work under Java 1.4.
    The simple application below should show fonts with a normal face,
    bold, italic and underline. Underline seems to work on all systems,
    but on both Windows and OS/X, bold and italic fail on many fonts. The
    same fonts seem to have bold and italic faces in non-Java
    applications, so it appears to be a problem with the JVM. For example,
    STENCIL on the Mac and Century on Windows both have distinct bold and
    italic faces in Word, but not in Java.

    On OS/X, many fonts do not display the proper face; they seem to
    default to serif or sans serif. Most of the Lucida fonts show this
    problem.

    Mike Westerfield

    --- main.java ---

    package fonttest;

    public class Main {

    public Main() {
    Window window = new Window();
    window.show();
    }
    public static void main(String[] args) {
    Main main1 = new Main();
    }
    }

    --- window.java ---

    package fonttest;

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;

    public class Window extends JFrame {
    private JScrollPane jScrollPane1 = new JScrollPane();
    private JTextPane jTextPane = new JTextPane();

    public Window() {
    try {
    jbInit();
    pack();
    populate();
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    private void jbInit() throws Exception {
    jScrollPane1.setPreferredSize(new Dimension(640, 480));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("Font Test");
    this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTextPane, null);
    }

    private void populate () {
    String defaultFont = "SanSerrif";
    String[] fontNames =
    GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    for (int f = 0; f < fontNames.length; ++f) {
    jTextPane.getCaret().setDot(Integer.MAX_VALUE);
    setFont(defaultFont, false, false, false);
    jTextPane.replaceSelection(fontNames[f] + "\t");

    jTextPane.getCaret().setDot(Integer.MAX_VALUE);
    setFont(fontNames[f], false, false, false);
    jTextPane.replaceSelection("Abcdef" + "\t");

    jTextPane.getCaret().setDot(Integer.MAX_VALUE);
    setFont(fontNames[f], true, false, false);
    jTextPane.replaceSelection("Abcdef" + "\t");

    jTextPane.getCaret().setDot(Integer.MAX_VALUE);
    setFont(fontNames[f], false, true, false);
    jTextPane.replaceSelection("Abcdef" + "\t");

    jTextPane.getCaret().setDot(Integer.MAX_VALUE);
    setFont(fontNames[f], false, false, true);
    jTextPane.replaceSelection("Abcdef" + "\n");
    }
    }
    private void setFont(String name, boolean bold, boolean italic,
    boolean underline) {
    SimpleAttributeSet font = new SimpleAttributeSet();
    font.addAttribute(StyleConstants.FontFamily, name);
    font.addAttribute(StyleConstants.FontSize, new Integer(16));
    font.addAttribute(StyleConstants.Bold, new Boolean(bold));
    font.addAttribute(StyleConstants.Italic, new Boolean(italic));
    font.addAttribute(StyleConstants.Underline, new
    Boolean(underline));
    jTextPane.setCharacterAttributes(font, false);
    }
    }
     
    Mike Westerfield, May 14, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stargazer

    Need a list of font faces

    Stargazer, Jan 5, 2006, in forum: HTML
    Replies:
    6
    Views:
    2,822
    Andy Dingley
    Jan 5, 2006
  2. The Doctor

    Font Styles

    The Doctor, Oct 10, 2006, in forum: HTML
    Replies:
    5
    Views:
    1,026
    Jukka K. Korpela
    Oct 12, 2006
  3. Hans Stoessel

    How can I get all font faces?

    Hans Stoessel, Jan 14, 2008, in forum: Java
    Replies:
    8
    Views:
    2,415
    Jeffrey H. Coffield
    Jan 16, 2008
  4. Nicolas Verhaeghe

    Mixing font styles in a dropdownlist

    Nicolas Verhaeghe, Jun 22, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    983
    Alexey Smirnov
    Jun 22, 2009
  5. mttc
    Replies:
    2
    Views:
    2,407
    Roedy Green
    Jul 3, 2009
Loading...

Share This Page