Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to Get a Monospaced Font

Reply
Thread Tools

How to Get a Monospaced Font

 
 
John B. Matthews
Guest
Posts: n/a
 
      11-27-2010
In article <icrirs$mle$(E-Mail Removed)>,
Martin Gregorie <(E-Mail Removed)> wrote:

> On Sat, 27 Nov 2010 11:33:47 -0500, John B. Matthews wrote:
>
> > In article <icr2ud$i91$(E-Mail Removed)>,
> > Martin Gregorie <(E-Mail Removed)> wrote:
> >
> >> Agreed, but AFAIK the layout manager doesn't set the width of the
> >> JTextField and JTextArea text entry box if you use the constructor
> >> that specifies it as a character count. As the risk of appearing
> >> boring, I'd again point out that doing so always to make a text
> >> area thats about 220% of the required width. I'd like to know why
> >> and where this algorithm is documented. Is it buggy?

> >
> > Excellent question: "By default this is defined to be the width of
> > the character m for the font used." Peeking at the implementation,
> > I see getPreferredSize() calls getColumnWidth():
> >
> > <http://download.oracle.com/javase/6/docs/api/javax/swing/JTextField.html>
> >

> Interestingly getColumnWidth() is said to be redefinable to return
> some other value, but as there is no method for setting a different
> column width I don't see how you'd do that apart from extending the
> class and overriding both it and getPreferredSize().


I've never had occasion to override getColumnWidth(), but I guess
that's what "redefine" implies. Can one infer from the wording that
getPreferredSize() calls getColumnWidth()?

This may be one of those cases where "design and document for
inheritance" could be more explicit.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-28-2010
On 11/26/2010 04:02 PM, KevinSimonson wrote:
> I'm trying to draw strings to a<JPanel>, and would like them to be
> drawn in a font where each character has the same pixel width. I had
> thought that "Courier" was such a font, so I wrote the following
> program to verify that its characters do in fact have the same pixel
> width, but when I tried running it I saw that they did not. For
> example, the small "i" is very much narrower than the capital "W".
> Can someone tell me a font I can use that might have a
> chance of being monospaced?
>
> I think I asked something similar to this before, and somebody told me
> that different machines have different fonts, so I couldn't count on
> getting an answer that was generally applicable. If that is true, how
> can I find out which fonts my machine has? Any information would be
> greatly appreciated.
>
> Kevin S


I have never had a problem with Font.MONOSPACED giving me something
other than an monospaced font but it will be mapped to some
indeterminate physical font. Courier can be tricky because there are
Couriers that aren't monospaced. There are numerous monospaced fonts on
every machine I've ever used however. One thing to keep in mind though,
is that you can package fonts with your application. There are methods
of Font to load a font from a file. TrueType fonts are usable on either
Windows or Linux operating systems.

I had a job a few years back that required some drawing on a JPanel with
a monospaced font that wasn't present in the machines we were using. We
originally started loading all the machines with the font but then
discovered that we could just load the font from a file and in the end
that was much easier.

--

Knute Johnson
s/nospam/knute2010/
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-28-2010
On 11/26/2010 04:02 PM, KevinSimonson wrote:
> I'm trying to draw strings to a<JPanel>, and would like them to be
> drawn in a font where each character has the same pixel width. I had
> thought that "Courier" was such a font, so I wrote the following
> program to verify that its characters do in fact have the same pixel
> width, but when I tried running it I saw that they did not. For
> example, the small "i" is very much narrower than the capital "W".
> Can someone tell me a font I can use that might have a
> chance of being monospaced?
>
> I think I asked something similar to this before, and somebody told me
> that different machines have different fonts, so I couldn't count on
> getting an answer that was generally applicable. If that is true, how
> can I find out which fonts my machine has? Any information would be
> greatly appreciated.
>
> Kevin S


I have never had a problem with Font.MONOSPACED giving me something
other than an monospaced font but it will be mapped to some
indeterminate physical font. Courier can be tricky because there are
Couriers that aren't monospaced. There are numerous monospaced fonts on
every machine I've ever used however. One thing to keep in mind though,
is that you can package fonts with your application. There are methods
of Font to load a font from a file. TrueType fonts are usable on either
Windows or Linux operating systems.

I had a job a few years back that required some drawing on a JPanel with
a monospaced font that wasn't present in the machines we were using. We
originally started loading all the machines with the font but then
discovered that we could just load the font from a file and in the end
that was much easier.

--

Knute Johnson
s/nospam/knute2010/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-28-2010
On Fri, 26 Nov 2010 16:02:13 -0800 (PST), KevinSimonson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>I think I asked something similar to this before, and somebody told me
>that different machines have different fonts, so I couldn't count on
>getting an answer that was generally applicable. If that is true, how
>can I find out which fonts my machine has? Any information would be
>greatly appreciated.


see http://mindprod.com/jgloss/logicalfonts.html

what you want is Monospaced in Java and monospace for CSS.

See http://mindprod.com/applet/fontshower.html
to see the official names of what fonts are available on any given
machine, and what they look like and roughly what characters they
support.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-28-2010
On Fri, 26 Nov 2010 16:02:13 -0800 (PST), KevinSimonson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>I think I asked something similar to this before, and somebody told me
>that different machines have different fonts, so I couldn't count on
>getting an answer that was generally applicable. If that is true, how
>can I find out which fonts my machine has? Any information would be
>greatly appreciated.


see http://mindprod.com/jgloss/logicalfonts.html

what you want is Monospaced in Java and monospace for CSS.

See http://mindprod.com/applet/fontshower.html
to see the official names of what fonts are available on any given
machine, and what they look like and roughly what characters they
support.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-28-2010
On Fri, 26 Nov 2010 17:40:35 -0800, markspace <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>
>I haven't had time to experiment with this personally, but something in
>here might help:
>
><http://download.oracle.com/javase/1.3/docs/guide/intl/physicalfont.html>


note there is an error on that page
font.ITALIC should read Font.ITALIC

Also the names of the fonts are wrong on the page..

see http://mindprod.com/jgloss/physicalfonts.html
http://mindprod.com/jgloss/bundledfonts.html
http://mindprod.com/jgloss/freefonts.html
http://mindprod.com/jgloss/downloadablefonts.html



--
Roedy Green Canadian Mind Products
http://mindprod.com

If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-29-2010
On Fri, 26 Nov 2010 16:02:13 -0800 (PST), KevinSimonson
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>I think I asked something similar to this before, and somebody told me
>that different machines have different fonts, so I couldn't count on
>getting an answer that was generally applicable. If that is true, how
>can I find out which fonts my machine has? Any information would be
>greatly appreciated.


see http://mindprod.com/jgloss/logicalfonts.html

what you want is Monospaced in Java and monospace for CSS.

See http://mindprod.com/applet/fontshower.html
to see the official names of what fonts are available on any given
machine, and what they look like and roughly what characters they
support.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you give your kitchen floor a quick steam mop every few days, you will find you never have to get out buckets and brushes for deep cleaning. Similary, if you keep your code tidy, refactoring as you go, you probably won't need major rewrites.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Swing Font, it's Java Font? ot native? how install new font? mttc Java 2 07-03-2009 07:29 PM
&ensp in a monospaced font Andy Fish HTML 98 03-06-2008 01:53 PM
How to get the text in html tag.like<div...><font...>Text</font></ =?Utf-8?B?Tmlja3k=?= ASP .Net 2 02-20-2005 03:03 PM
monospaced fonts good for programming. For those bored to tears with the same ol' @#$% Thomas G. Marshall Java 2 08-01-2004 01:52 AM



Advertisments