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?



Relevant Pages

  • Re: Flickering as paint continually recalled due to setFont call
    ... > The Windows implementation of the Java virtual machine is causing my ... observer any continuous flickering. ... either a new bug or feature with the way JFrame paints. ... then it takes a while to create the new font object and draw the new string. ...
    (comp.lang.java.gui)
  • Dynamic sizing of windows
    ... I've been trying to dynamically re-size a frame, according to what font ... I cannot get the width of an 80 characters long string until I have ... created a JPanel to add to my JFrame, which is too late for resizing. ...
    (comp.lang.java.gui)
  • Autoscaling in Java2d
    ... Here's the code I'm using to populate the JLabel: ... Tile tile; ... figure out how to have the font-size automatically scale in a beautiful crisp ... the Outline.java font demo's fonts do exactly ...
    (comp.lang.java.gui)
  • Re: Multiple languages in one JLabel
    ... > label. ... A JLabel has no way of setting the font at predetermined points during its ... paint routine. ...
    (comp.lang.java.programmer)
  • Re: JFrame does not automatically resizes to JLabels length
    ... > I am using three JLabel's on a row using the BorderLayout. ... > JLabel must be aware of the JFrame, ... getPreferredSize() will be called which so long ...
    (comp.lang.java.gui)