Calculating font size

From: Larry Coon (lmcoon_at_nospam_cox.net)
Date: 11/15/03


Date: Fri, 14 Nov 2003 18:49:10 -0800

I have a JFrame that contains just a JLabel. The
text in the JLabel may change.

I want to reset the size of the Font in the JLabel
whenever the JFrame is resized. The font size should
be the largest that will fit the entire string within
the resized window.

I'm setting the size inside a ComponentListener, and
I'm looking for an algorithm or API method to determine
the size to use. eg:

addComponentListener(new ComponentAdapter() {
  public void componentResized(ComponentEvent e) {
    Component c = e.getComponent();
    int windowHeight = c.getHeight();
    int windowWidth = c.getWidth();
    String text = label.getText(); // label is the JLabel.

    int fontSize = // Need algorithm here.

    display.setFont(new Font("Serif", Font.PLAIN, fontSize));
  }
});

Is there a good way to calculate the font size?