Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Unicode labels, fonts for components?

Reply
Thread Tools

Unicode labels, fonts for components?

 
 
Peter Duniho
Guest
Posts: n/a
 
      01-21-2008
The main problem I'm having is that the "←" (left arrow) and "→" (right
arrow) characters are not displaying property under Windows (Java 6).
They are displayed correctly on the Mac (Java 5). (I mentioned the JRE
version in case it's relevant, but I'd hope it wouldn't be).

I wondering if this is a consequence of me not setting some property or
setting or otherwise not configuring my application or Java correctly. If
so, how do I get it to work? If not, what might be wrong? Is it a known
limitation of the Windows Java implementation?

I had expected that Java would be fully Unicode-enabled. Eclipse seems to
support it just fine, under Windows and on the Mac. And the Java runtime
on the Mac supports it fine. (In fact, even on the Eclipse version of
Windows the arrows show up fine in the source, and since Eclipse is
practically all Java (right?) I expect that demonstrates that there is
_some_ way to get Unicode characters to work right in a Java application).

Related to this is that Component.getFont() doesn't appear to behave as
documented. The docs imply that _some_ font will be returned; if the
Component doesn't have one set, the parent's font will be returned. Now,
I suppose read literally this means that if the parent doesn't have a
font, you get null. But I would have thought that "font will be returned"
really means "the result of Component.getFont() on the parent will be
returned", implying that it will work its way up the containment hierarchy
until a font is found and returned.

I ran into this because I wondered if the arrows weren't displaying
properly because of a font issue. The font being displayed _looks_ like
Arial, but my installed Arial font has those characters so I figured I'd
double-check to see what font was being used. But calling getFont() on
the Component (it's a Button...not sure if that matters) returns null,
which is not at all what I expected.

Is there any way programmatically to reliably get the actual font that's
current being used to display text in a Component?

But mostly I just want to know how to get my arrow characters to display
properly.

Thanks!
Pete
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-21-2008
Peter Duniho wrote:
> The main problem I'm having is that the "←" (left arrow) and "→" (right
> arrow) characters are not displaying property under Windows (Java 6).
> They are displayed correctly on the Mac (Java 5). (I mentioned the JRE
> version in case it's relevant, but I'd hope it wouldn't be).
>
> I wondering if this is a consequence of me not setting some property or
> setting or otherwise not configuring my application or Java correctly.
> If so, how do I get it to work? If not, what might be wrong? Is it a
> known limitation of the Windows Java implementation?
>
> I had expected that Java would be fully Unicode-enabled. Eclipse seems
> to support it just fine, under Windows and on the Mac. And the Java
> runtime on the Mac supports it fine. (In fact, even on the Eclipse
> version of Windows the arrows show up fine in the source, and since
> Eclipse is practically all Java (right?) I expect that demonstrates that
> there is _some_ way to get Unicode characters to work right in a Java
> application).
>
> Related to this is that Component.getFont() doesn't appear to behave as
> documented. The docs imply that _some_ font will be returned; if the
> Component doesn't have one set, the parent's font will be returned.
> Now, I suppose read literally this means that if the parent doesn't have
> a font, you get null. But I would have thought that "font will be
> returned" really means "the result of Component.getFont() on the parent
> will be returned", implying that it will work its way up the containment
> hierarchy until a font is found and returned.
>
> I ran into this because I wondered if the arrows weren't displaying
> properly because of a font issue. The font being displayed _looks_ like
> Arial, but my installed Arial font has those characters so I figured I'd
> double-check to see what font was being used. But calling getFont() on
> the Component (it's a Button...not sure if that matters) returns null,
> which is not at all what I expected.
>
> Is there any way programmatically to reliably get the actual font that's
> current being used to display text in a Component?
>
> But mostly I just want to know how to get my arrow characters to display
> properly.
>


The following displays fine on Windows XP with JRE 1.6

--------------------------8<---------------------------------
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGetFont {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestGetFont();
}
});
}

TestGetFont() {
String arrows = "left \u2190 right \u2192 arrows";

JButton b = new JButton(arrows);
System.out.println("JButton font is " + b.getFont());

JPanel p = new JPanel();
p.add(b);

JFrame f = new JFrame("Test Component.getFont()");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

}
--------------------------8<---------------------------------

The console output from println is:
JButton font is
javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=bold,size=12]
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-21-2008
RedGrittyBrick wrote:
> Peter Duniho wrote:
>> The main problem I'm having is that the "←" (left arrow) and "→"
>> (right arrow) characters are not displaying property under Windows
>> (Java 6). They are displayed correctly on the Mac (Java 5).
>>
>> I wondering if this is a consequence of me not setting some property
>> or setting or otherwise not configuring my application or Java
>> correctly. If so, how do I get it to work?
>>

>


<My SSCCE snipped>

P.S.

I can replace
String arrows = "left \u2190 right \u2192 arrows";
with
String arrows = "left ← and right → arrows";

But I first had to change Eclipse's default text-file-encoding from the
default CP1252 to UTF8 in Window -> Preferences -> General -> Workspace.

When I did this, Eclipse recompiled everything in my workspace and made
a mess of some non-ASCII Latin-1 accented characters in one string
constant in one source file. I was able to simply paste in replacement
text from a backup, but you might want to prepare for this if your
workspace has many Java source code files that contain non-ASCII characters.

For me, this sort of problem is a strong argument in favour of using
Unicode escapes such as \u2190.

I expect anyone in a non-English locale has already got Eclipse set up
so that usage of non-ASCII characters in Java source code isn't a problem.
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      01-22-2008
On Mon, 21 Jan 2008 02:30:00 -0800, RedGrittyBrick
<(E-Mail Removed)> wrote:

> The following displays fine on Windows XP with JRE 1.6


Thanks for the demo code! I'll try that sample when I get a chance.
However, note that I'm not using Swing. It's a plain AWT Button, not a
JButton.

Is this something that is simply not expected to work with AWT, but will
with Swing?

Can anyone else comment on the AWT side of the question?

Thanks,
Pete
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      01-22-2008
Peter Duniho wrote:
> On Mon, 21 Jan 2008 02:30:00 -0800, RedGrittyBrick
> <(E-Mail Removed)> wrote:
>
>> The following displays fine on Windows XP with JRE 1.6

>
> Thanks for the demo code! I'll try that sample when I get a chance.
> However, note that I'm not using Swing. It's a plain AWT Button, not a
> JButton.


That explains a lot. AWT punts everything to the OS in terms of how
stuff works; fonts are no exception. Try reading Sun's i18n FAQ. (In
short: blame Microsoft for your problems)

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      01-22-2008
On Mon, 21 Jan 2008 18:44:27 -0800, Joshua Cranmer
<(E-Mail Removed)> wrote:

> That explains a lot. AWT punts everything to the OS in terms of how
> stuff works; fonts are no exception. Try reading Sun's i18n FAQ. (In
> short: blame Microsoft for your problems)


As far as I know, Microsoft didn't write the JRE implementation. I don't
see how they are responsible. It's not their fault Sun documented
something they can't deliver (assuming what you write is correct).

If this is a limitation in AWT, then the docs should say so. I think it's
pretty silly for the docs to imply that getFont() will always return a
font when in fact it doesn't.

I appreciate attempts to answer the actual question, but I don't see how
the finger-pointing is constructive or helpful at all. Either the docs
describe what happens or they don't. I'm less interested in why
(especially when it involves subjective blame games), and more interested
in whether I can get Unicode characters to be correctly displayed in my
Button control and if so, how (the font issue is actually secondary to my
main question anyway).

Thanks,
Pete
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-22-2008
On Sun, 20 Jan 2008 23:56:27 -0800, "Peter Duniho"
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>But mostly I just want to know how to get my arrow characters to display
>properly.


You can do a setFont for any font installed. So long as it has
unicode support for the chararters you want, you should be in good
shape.

See http://mindprod.com/applet/fontshower.html
to preview what various font capabilities are.

see also http://mindprod.com/jgloss/font.html#LOGICAL
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      01-22-2008
On Mon, 21 Jan 2008 20:40:54 -0800, Roedy Green
<(E-Mail Removed)> wrote:

>> But mostly I just want to know how to get my arrow characters to display
>> properly.

>
> You can do a setFont for any font installed. So long as it has
> unicode support for the chararters you want, you should be in good
> shape.


Well, there are certainly fonts installed (and ubiquitous enough) that
include the characters I want. But they seem not to be used by default,
and I'm confused by your own advice as to whether I can actually use them
or not. See below...

> See http://mindprod.com/applet/fontshower.html
> to preview what various font capabilities are.
>
> see also http://mindprod.com/jgloss/font.html#LOGICAL


Thanks!

According to this link, the font being used on Windows should be "Lucida
Sans". And sure enough, the characters I want are not in that font, in
spite of your statement on the page that "these Java logical fonts support
many more characters than most fonts". Apparently the arrows are not
among the "miscellanous symbols" included ...in fact, looking at the
font, at least according to the Character Map application, the font has
relatively few characters in it, and no Chinese characters at all
(something your page specifically mentioned as being included).

Interestingly, there's also a "Lucida Sans Unicode" font installed that
_does_ have the arrows (but no Chinese characters still).

But: your page also says that I can only use the five Java logical fonts
with an AWT control. This conflicts with your statement above, "You can
do a setFont for any font installed". Can I actually do that? Or am I,
as the web page says, restricted to the five Java fonts?

Pete

p.s. By the way, that page took forever to load in Opera, and never did
finish downloading the last of 85 images (the brower's status shows it
stuck at "Image: 84/85"). Not sure if you're concerned about that or not.
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      01-22-2008
On Mon, 21 Jan 2008 21:09:45 -0800, Peter Duniho
<(E-Mail Removed)> wrote:

> [...]
> But: your page also says that I can only use the five Java logical fonts
> with an AWT control. This conflicts with your statement above, "You can
> do a setFont for any font installed". Can I actually do that? Or am I,
> as the web page says, restricted to the five Java fonts?


Okay, I'd love to get a more definitive answer, but just for the record, I
was unable to set the font to something that would display the arrow
characters while using an AWT control.

I tried using a Swing JButton instead, and it works fine by default. I
didn't even have to set the font. The "look and feel" is actually a
little off on Windows XP...it has more of a Vista look. And on the Mac,
the auto-sizing of the button is a little different, but otherwise it
looks okay.

Maybe I should just not worry so much about the question of using
native-peer AWT controls (for best user-experience consistency) and go
with the Swing controls instead.

I'd still like to know if it's possible to do this somehow with AWT. In
particular, is it really true that I can set the font to any font I need?
If so, how?

Thanks!
Pete
 
Reply With Quote
 
Wayne
Guest
Posts: n/a
 
      01-22-2008
Peter Duniho wrote:
> On Mon, 21 Jan 2008 21:09:45 -0800, Peter Duniho
> <(E-Mail Removed)> wrote:
>
>> [...]

>
> I'd still like to know if it's possible to do this somehow with AWT. In
> particular, is it really true that I can set the font to any font I
> need? If so, how?
>
> Thanks!
> Pete


Fonts support a "canDisplay" method. You can use this to iterate over
available fonts to determine a set that displays the characters
you wish.

Here's some code. While I used swing (so my applet could have
a menu) I think this would work for AWT as well:

<snippit>
// Find all Fonts that can display arrow symbols:
fontList = new ArrayList<Font>();
Collections.addAll( fontList,
GraphicsEnvironment.getLocalGraphicsEnvironment(). getAllFonts() );
for (Iterator<Font> i = fontList.iterator(); i.hasNext(); ) {
Font f = i.next();
if ( ! f.canDisplay( '\u2192' ) ) // right arrow
i.remove();
}
</snippit>

Here's a link to the full applet and code:
http://www.hccfl.edu/pollock/Java/UnicodeSymbols.htm

Hope this helps!

-Wayne
 
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
Unicode fonts in Java maxwell@ldc.upenn.edu Java 3 03-20-2007 05:17 PM
Fonts suddenly unreadable in IE 6.0 - All other system fonts appear normal Emily Computer Support 3 06-09-2005 04:52 AM
Swithing Unicode fonts Nik Firefox 3 04-30-2005 11:05 AM
asp fonts problems (chinese fonts become ??????) sam ASP General 0 06-06-2004 06:15 AM
Re: Unicode fonts in Windows Mike Computer Support 2 07-22-2003 05:49 PM



Advertisments