Multiple languages in one JLabel

E

Elly

I'm writing a utility that includes selecting and displaying files from the
desktop.

I'm running into problems since windows xp can have filenames made up of
characters from multiple languages.

How does one display such a string in a JLabel?

I thought of using a large font like MS Arial Unicode, but it isn't
distributable and the post I read mentioned that no large font exists to
handle all of the unicode character set in a complete manner for native
users of the various unicode characters.

Help?

-Elly
 
M

Michael Borgwardt

Elly said:
I'm writing a utility that includes selecting and displaying files from the
desktop.

I'm running into problems since windows xp can have filenames made up of
characters from multiple languages.

How does one display such a string in a JLabel?

Just like any other string. Makes no difference to Java.
I thought of using a large font like MS Arial Unicode, but it isn't
distributable and the post I read mentioned that no large font exists to
handle all of the unicode character set in a complete manner for native
users of the various unicode characters.

I think you can assume that the default font will be able to display all
characters that a user in the default locale is likely to user. Windows
itself has the same problem, after all.
 
E

Elly

Just like any other string. Makes no difference to Java.

Java doesn't care about storing or using strings, but JLabel appears to
be incapable of displaying characters of different fonts within a single
label.
I think you can assume that the default font will be able to display
all characters that a user in the default locale is likely to user.
Windows itself has the same problem, after all.

In windows, a user can create filenames of mixed characters from
different languages.
These characters are all visible together in one filename on the windows
desktop.
If my Java app tries to display this file, it is unable to show all the
characters of different languages next to each other.
You have to choose one font or another for the JLabel, leaving square
boxes for the other characters.


-Elly
 
V

VisionSet

Java doesn't care about storing or using strings, but JLabel appears to
be incapable of displaying characters of different fonts within a single
label.


You change the font on the graphics object that is used to paint the
component.
A JLabel has no way of setting the font at predetermined points during its
paint routine.
However if you subclassed JLabel and overrode its paintComponent method then
you can do what you like!
 
A

Andrew Thompson

..JLabel appears to
be incapable of displaying characters of different fonts within a single
label.

<sscce>
import java.awt.*;
import javax.swing.*;

/** Displays multiple fonts in a single JLabel using HTML formatting. */
public class MultiFontLabel {

public static void main(String args[]) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Font allFonts[] = ge.getAllFonts();

StringBuffer sb = new StringBuffer();
sb.append( "<html><body><ul>" );
for (int ii=0; ii<allFonts.length; ii++) {
sb.append( "<li><font face='" + allFonts[ii].getFamily() +
"'>" + allFonts[ii].getFamily() + "</font>");
}
sb.append( "</ul></body></html>" );

JLabel l = new JLabel(sb.toString());
JScrollPane sp = new JScrollPane(l);
sp.setPreferredSize(new Dimension(300, 500));
JOptionPane.showMessageDialog(null, sp);
}
}
</sscce>
 
E

Elly

..JLabel appears to
be incapable of displaying characters of different fonts within a
single label.

<sscce>
import java.awt.*;
import javax.swing.*;

/** Displays multiple fonts in a single JLabel using HTML formatting.
*/ public class MultiFontLabel {

public static void main(String args[]) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Font allFonts[] = ge.getAllFonts();

StringBuffer sb = new StringBuffer();
sb.append( "<html><body><ul>" );
for (int ii=0; ii<allFonts.length; ii++) {
sb.append( "<li><font face='" + allFonts[ii].getFamily() +
"'>" + allFonts[ii].getFamily() + "</font>");
}
sb.append( "</ul></body></html>" );

JLabel l = new JLabel(sb.toString());
JScrollPane sp = new JScrollPane(l);
sp.setPreferredSize(new Dimension(300, 500));
JOptionPane.showMessageDialog(null, sp);
}
}
</sscce>

Ahh, most excellent!

Much thanks Andrew T and Mike W

-Elly
 
V

VisionSet

A JLabel has no way of setting the font at predetermined
points during its paint routine.

Except if you use JTextComponents html abilities of course ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top