Set font size based on component size

Hi everybody,

This is kind of a duplicate, kind of new, because my original post is buried and I figured out a more helpful and general way to ask this question. Does anyone know if there is a way to set the size of a string as large as possible on a component, but still be able to have the whole string visible, if you're using a font other than default?

For example:

publicstaticvoid main( String args[] ){

try{

JFrame frame =new JFrame("Title" );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

int guiWidth = width / 2;

int guiHeight = height / 2;

frame.setSize( guiWidth, guiHeight );

Container pane = frame.getContentPane();

// Create main label

String labelString ="Test";

JLabel label =new JLabel( labelString );

label.setBackground(new Color( 179, 0, 0 ) );

label.setForeground(new Color( 235, 191, 16 ) );

label.setHorizontalAlignment( CENTER );

label.setOpaque(true );

label.setFont(new Font( 揙ld English Text MT? 0, 48 ) );

pane.add( label, BorderLayout.NORTH );

frame.setLocationRelativeTo(null );

frame.setVisible(true );

}catch( Exception e){

e.printStackTrace();

}

}

As you can see, right now the font needs to be hard coded. I'd like it to be dynamic, based on the size of the screen the user has.

Sorry if the partial double post annoys anybody; I figured it would be less annoying than resurrecting my old post.

Any help on this would be great, and thank you!

Jezzica85

Message was edited by:

jezzica85

Message was edited by:

jezzica85

[2419 byte] By [jezzica85a] at [2007-11-27 8:55:53]
# 1

import java.awt.*;

import java.awt.event.*;

import java.awt.font.*;

import java.awt.geom.AffineTransform;

import javax.swing.*;

public class DynamicFont {

private JLabel getNorth() {

// Create main label

String labelString = "Test";

final JLabel label = new JLabel( labelString ) {

public void paint(Graphics g) {

((Graphics2D)g).setRenderingHint(

RenderingHints.KEY_TEXT_ANTIALIASING,

RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

super.paint(g);

}

};

label.setBackground( new Color( 179, 0, 0 ) );

label.setForeground( new Color( 235, 191, 16 ) );

label.setHorizontalAlignment( JLabel.CENTER );

label.setOpaque( true );

label.setFont( new Font( "Old English Text MT", 0, 48 ) );

label.addComponentListener(new ComponentAdapter() {

Font font = label.getFont();

public void componentResized(ComponentEvent e) {

int w = label.getWidth();

int sw = label.getFontMetrics(font).stringWidth(label.getText());

double scale = (double)w/sw;

AffineTransform at = AffineTransform.getScaleInstance(scale, 1.0);

label.setFont(font.deriveFont(at));

label.repaint();

}

});

return label;

}

public static void main( String[] args ) {

JFrame frame = new JFrame( "Title" );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

Container pane = frame.getContentPane();

pane.add( new DynamicFont().getNorth(), BorderLayout.NORTH );

frame.setSize( 400, 100);

frame.setLocationRelativeTo( null );

frame.setVisible( true );

}

}

crwooda at 2007-7-12 21:17:53 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you crwood, but when I tried this the label text was all stretched horizontally--I think it might have had something to do with the scale factor; is there a way to make that dynamic too?

This also doesn't seem to work for all screen resolutions--sometimes the font overflows off the label, and sometimes it doesn't show up at all.

Thanks,

Jezzica85

Message was edited by:

jezzica85

Message was edited by:

jezzica85

jezzica85a at 2007-7-12 21:17:53 > top of Java-index,Desktop,Core GUI APIs...
# 3

but when I tried this the label text was all stretched horizontally

I was trying to do this:

a way to set the size of a string as large as possible on a component, but still be able

to have the whole string visible

Maybe I misunderstood.

doesn't seem to work for all screen resolutions

Can't help you with this part.

crwooda at 2007-7-12 21:17:53 > top of Java-index,Desktop,Core GUI APIs...