• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can i identify the total number of fonts be fit to my screen resolution in one line only?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys,

I have a new challenge to set a big line including hundred numbers of characters into one line only. I know that in one line 80 characters can be stored. but here i have a facility to change the type of fonts and size of fonts also.
Than how can i identify that how many characters can be stored in one line with the selected font style and size?

is it possible to do that in Swing application?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes it is. But there's a lot of obscure classes involved. I wrote code like that about five years ago and I haven't looked at it since, so I'll just give a brief outline.

First you have to put your hundred-number string into an AttributedString along with the details of the Font you plan to use. That includes the size of the Font, so you will want to repeat this with different Font sizes until you find one which works.

Next get the width of the component you want to display the string into. You should be able to get a Dimension object from the component and then get its width.

Finally you need some code like this:



At least I think that's right, I've extracted that snippet from a more complicated bit of code which is supposed to wrap the text into a block of fixed width. So you will probably have to play around with it. You might also want to search out a tutorial dealing with those classes, because that's most likely where I found out about them.

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend going with FontMetrics
A working example can be found here https://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html
Also check out the convenient static method SwingUtilities#computeStringWidth(FontMetrics fontMetrics, String stringToMeasure);
 
reply
    Bookmark Topic Watch Topic
  • New Topic