how to read the space

Hello,Just curious about one thing. how can i say that examplewhich text entered by the user in the text area takes less space than the other text ?Hi ij JK (ij takes less space thanJK) any idea?
[223 byte] By [lrngjavaa] at [2007-11-27 2:40:09]
# 1
If I understand you correctly, look into the FontMetrics class. http://java.sun.com/j2se/1.5.0/docs/api/java/awt/FontMetrics.html
CaptainMorgan08a at 2007-7-12 3:02:49 > top of Java-index,Java Essentials,New To Java...
# 2
In my experience, and this may have changed since I tried it last, spaces are not computed accurately by font metrics. I had to substitute the width of another letter for a space to make what I wanted to do work.
Tavea at 2007-7-12 3:02:49 > top of Java-index,Java Essentials,New To Java...
# 3
captain i think this is what i want (FontMetrics)but why cannot i do this.FontMetrics metrics = new FontMetrics(); secondly tave how did you figure out the height and width of a text then?
lrngjavaa at 2007-7-12 3:02:49 > top of Java-index,Java Essentials,New To Java...
# 4
i am using swing a textField in which a user will enter a text and then if he/she clicks the button it will give him/her the output that this word is bigger than the other word.
lrngjavaa at 2007-7-12 3:02:49 > top of Java-index,Java Essentials,New To Java...
# 5

> captain i think this is what i want (FontMetrics)

> but why cannot i do this.

>

> FontMetrics metrics = new FontMetrics();

>

> secondly tave how did you figure out the height and

> width of a text then?

Because the only constructor is:

FontMetrics

protected FontMetrics(Font font)

Creates a new FontMetrics object for finding out height and width

information about the specified Font and specific character glyphs in that

Font.

Parameters:

font - the Font

abillconsla at 2007-7-12 3:02:49 > top of Java-index,Java Essentials,New To Java...
# 6

class AddTextListener implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

FontMetrics metrics = textField.getFontMetrics(font);

int fontHeight = metrics.getHeight();

String text = textField.getText();

int textWidth = metrics.stringWidth(text);

resultLabel.setText(textWidth);//error is here

}

}

i know it will take string but how can i convert it into int? i tried Integer.ParseInt(resultLabel.getText()) but still not working because resultLabel is a string.

lrngjavaa at 2007-7-12 3:02:49 > top of Java-index,Java Essentials,New To Java...
# 7
Just add a ""+ ... resultLabel.setText(""+textWidth)
abillconsla at 2007-7-12 3:02:50 > top of Java-index,Java Essentials,New To Java...
# 8
thanks mr abill got it. i am new to swing and wanted to see how can you count the characters using FontMetrics. I appreciate it. everything works just fine. dukes for you adn captain.
lrngjavaa at 2007-7-12 3:02:50 > top of Java-index,Java Essentials,New To Java...
# 9
You're welcome and thanks back.
abillconsla at 2007-7-12 3:02:50 > top of Java-index,Java Essentials,New To Java...