title: How to access all fonts of a font family with Swing
body: <p>My installation of OS X 10.8 comes pre-installed with 11 fonts in the family <em>Helvetica Neue</em>. I'm trying to find a way to access the fonts with styles like <em>medium</em> or <em>condensed</em>, which cannot be represented by the bit-mask values <code>Font.BOLD</code> and <code>Font.ITALIC</code>.</p>
  
  <p><code>GraphicsEnvironment.getAllFonts()</code> returns <code>Font</code> objects for all these fonts but applying them using <code>JLabel.setFont()</code> seems to only use the styles representable with the mentioned bit-mask. This is shown on the left in the screenshot below, which compares it to a sample of all fonts when they are used in <em>TextEdit</em>.</p>
  
  <p>The same happens if a <code>Font</code> object is constructed using the font's full name or its PostScript name.</p>
  
  <p>Is there a way to use all those fonts, either by applying it to a <em>Swing</em> component or when painting to a <code>Graphics2D</code> (or <code>Graphics</code>) instance?</p>
  
  <p><img src="https://i.stack.imgur.com/vNlcR.png" alt="Output from the Swing application on the left, sample of all fonts on the right."></p>
  
  <p>Below is the code I used to produce the dialog in the above screenshot.</p>
  
  <pre><code>package fahrplan;
  
  import java.awt.*;
  import javax.swing.*;
  
  public class FontsMain {
      public static void main(String[] a) {
          GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
  
          JPanel contentPane = new JPanel();
          contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
  
          for (Font i : e.getAllFonts()) {
              String name = i.getFontName();
  
              if (name.startsWith("HelveticaNeue")) {
                  JLabel label = new JLabel(name);
  
                  label.setFont(i.deriveFont(18f));
  
                  contentPane.add(label);
              }
          }
  
          JFrame frame = new JFrame("Fonts");
          frame.setContentPane(contentPane);
          frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
          frame.pack();
          frame.setVisible(true);
      }
  }
  </code></pre>