Archived

This topic is now archived and is closed to further replies.

[java] Centering text

This topic is 5668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a String that I want to place in a box in my frame. Now I can manually adjust it until it looks right but then when I put a different sized string of text it''s not centered as it should be. Is there a function that can center text on an x axis? I''ve looked it setHorizontalText or something but I couldn''t get it to work right.

Share this post


Link to post
Share on other sites
If you are using awt you can get a FontMetrics object from the Graphics (Graphics.getFontMetrics()) object and use the FontMetrics.stringWidth(String) to get the width of the string.

Hope that helps....

:-)

Share this post


Link to post
Share on other sites