FontMetrics on uppercase string

Hi, I have a problem when getting the width of a string in my app using the method computeStringWidth() passing in an instance of the FontMetrics class.

Because my string is un uppercase it seems to give me an incorrect value back, looking through the forums it seems this is only the case for uppercase and the method works perfectly with lowercase Strings.

Does anyone have a solution to this problem?

Thanks

Alan

[446 byte] By [Alan1983a] at [2007-10-2 6:42:33]
# 1
> I have a problem when getting the width of a string in my app using the method computeStringWidth() I'm not aware of a computStringWidth() method.I just use stringWidth(...) and it works fine.
camickra at 2007-7-16 13:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 2
The computeStringWidth() method is from the SwingUtilities class. It seems to perform the same as the stringWidth() method you mentioned. Seems to return a value too small for the string when the string is in uppercase.Any Ideas?ThanksAlan
Alan1983a at 2007-7-16 13:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 3
How to create a [url http://www.physci.org/codes/sscce.jsp]Short, Self Contained, Correct (Compilable), Example[/url].
camickra at 2007-7-16 13:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

Looks OK to me!

import javax.swing.*;

import java.awt.*;

public class Fred2 extends JFrame

{

public Fred2()

{

super("ZZZ");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel()

{

public void paintComponent(Graphics g)

{

String test = "hello world";

String test1 = "HELLO WORLD";

FontMetrics fm = g.getFontMetrics();

System.out.println(fm.stringWidth(test) + " " + SwingUtilities.computeStringWidth(fm, test));

System.out.println(fm.stringWidth(test1) + " " + SwingUtilities.computeStringWidth(fm, test1));

int w = fm.stringWidth(test);

g.setColor(Color.blue);

g.drawString(test, 0, 20);

g.setColor(Color.red);

g.drawLine(w,0,w,getHeight());

w = fm.stringWidth(test1);

g.setColor(Color.blue);

g.drawString(test1, 0, 40);

g.setColor(Color.red);

g.drawLine(w,0,w,getHeight());

}

};

panel.setPreferredSize(new Dimension(400,200));

setContentPane(panel);

pack();

}

public static void main(String[] args)

{

new Fred2().setVisible(true);

}

}

sabre150a at 2007-7-16 13:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 5
The idea was for the OP to post a demo program.
camickra at 2007-7-16 13:50:54 > top of Java-index,Desktop,Core GUI APIs...
# 6
> The idea was for the OP to post a demo program.I read his mind!
sabre150a at 2007-7-16 13:50:54 > top of Java-index,Desktop,Core GUI APIs...