File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Determining width and height of a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Determining width and height of a String" Watch "Determining width and height of a String" New topic

Determining width and height of a String

kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Hello Folks,
I have written an application that manages the x and y position of a String object through two seperate JSlider objects. The Display Area for the String object is a subclass of the JPanel class. The content of the String object is updated through a JButton and JTextField objects. I was successful in writing the code up until where i could get the String from TextField and move it around using the Horizontal and vertical sliders. My problem here is as i move the sliders to it maximum value the string disappears out of the drawing area which according to my program should not disappear from the drawing area. Are there any methods that would keep the String from not disappearing the drawing area. Below is my code: It is very lengthy. My code compiles fine and executes too except for the above problem.

Thank you,
[ July 24, 2005: Message edited by: kanaka tam ]
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
You can determine the width an height of a string using the FontMetrics class.

An instance of FontMetrics can be acquired using the getFontMetrics method on component. You should pass the font that you are using to paint on you component (getFont). Then you can use the getStringBounds method to calculate the width and height of a string.

I do not know if this helps you out but it answers the question in your message header.
I agree. Here's the link:
subject: Determining width and height of a String
It's not a secret anymore!