473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trouble returning text height

23
I am trying to create custom buttons for an application. I have created a class that extends JButton and overridden the paintComponent() method. My problem is that I cannot find a way to return the pixel height of the text that will be put on the button, and therefore I am not able to center the text vertically within the button. I have tried something like this:

Expand|Select|Wrap|Line Numbers
  1. GlyphVector gv = myFont.createGlyphVector (g2d.getFontRenderContext(), string);
  2. double height = gv.getLogicalBounds().getHeight();
  3.  
  4. textLocationY = (myButton.getHeight() + height) / 2;
  5. g2d.drawString (string, textLocationX, textLocationY);
  6.  
I have also tried using FontMetric and calling getHeight, getAscent, getMaxAscent, etc... I can get the text almost centered, but it is still visibly off. I can center the text by hard-coding a couple of extra pixels, but that doesn't hold when the button is resized, or if I change the font size, and it's just bad programming practice. Any help would be greatly appreciated.
Jan 2 '09 #1
3 1473
JosAH
11,448 Expert 8TB
Get the ascent from the LineMetrics object given a piece of text using a Font (read the API documentation of the Font class). Note that long ascenders (e.g. h, k, l and the capital letters) cause the entire text to be visually positioned a bit lower than you expected because the majority of the pixels will be positioned below the average height. If you want to correct that you really have to do quite some ugly calculations.

kind regards,

Jos
Jan 3 '09 #2
HxRLxY
23
I found that using TextLayout and calling getBounds returns a fairly accurate bounding rectangle. Thank you for the help.
Jan 3 '09 #3
JosAH
11,448 Expert 8TB
@HxRLxY
Yes, a TextLayout object uses a LineMetrics object to do the dirty work.

kind regards,

Jos
Jan 3 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Fred Zilz | last post by:
I am trying to use an external style sheet to create a round cornered box. I am have difficulties correctly referencing my class in my style sheet. I am posting an excerpt from my style sheet: ...
14
by: webguru | last post by:
Okay, In the comps.language.javascript newsgroup, I was convinced to use CSS Rollovers instead of javascript. The first rollover went great. Although I can't get the text to align vertically in...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
0
by: V Arzola | last post by:
I've done extensive work developing a large on-line application form, only to find that it will not render properly in Firefox (1.5.0.1) or Netscape (8.0). The panels are overlapping each other...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
5
by: NYXX | last post by:
Here is my Html Code and my Contact.php This is My code in my html <td height="345" valign="top"> <form style="margin:0; padding:0; " action="contact.php" method="get" id="form"> <table...
3
by: Michellevt | last post by:
Hi I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes...
1
by: jeddiki | last post by:
Hi, I am having difficulty seeing why my divs will not swap places :confused: I want to swap the positions of the "Hot News" and the "Todays Bonuese" sections. Here is how they look now: ...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.