Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Smart String truncate

Thread view: 
Katie - 20 Apr 2007 16:52 GMT
Hi,

I am trying to write a truncate method which will be able to
concatenate a String based on pixel width of the string rather than
the number of characters. I was trying to use the Fontmetrics object
but unfortunately its abstract and get be used with the Graphics
(abstract) object which i don't have.
Is there any way to accomplish this without using the Graphics object,
to use the Font class to set the font styles and then passing the
string and getting its width based on the Font and styles ?

Thanks for your help
:)
Tom Hawtin - 20 Apr 2007 17:31 GMT
> I am trying to write a truncate method which will be able to
> concatenate a String based on pixel width of the string rather than
[quoted text clipped - 4 lines]
> to use the Font class to set the font styles and then passing the
> string and getting its width based on the Font and styles ?

java.awt.Font only contains information about font name, size, and
various other attributes. In order to get the FontMetrics for it, you
need to know how to acquire the font glyphs, how it's going to be
rendered (anti-aliasing implementations, for instance, often makes text
wider). How many pixels in 12pt Times Roman for the string "xyz"? Well,
it depends...

So, you need some context for this extra information. That is usually
what AWT provides through Graphics. Otherwise, you are left with
implementing FontMetrics yourself.

Tom Hawtin
Daniel Pitts - 20 Apr 2007 22:53 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> Thanks for your help
> :)

The number of pixels is based on how its going to be rendered.
Generally, you should truncate the text in the paintComponent method,
(where you actually have a Graphics object) rather than anywhere else.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.