开发者

Swing font names do not match? (Making a font chooser, and am trying to display the default system font in a JComboBox)

I am creating a swing font chooser. (See also: How to prevent JComboBox from becoming unresponsive when using a custom ListCellRenderer)

To get all available fonts, I do:

GraphicsEnvironment gE = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = gE.getAllFonts();

In my font chooser (a JComboBox), which contains all the available fonts, I want to initialize it showing the default system font as selected.

To do this, I use a static base font (which is gotten from a static base label (a JLabel)), from which I get the default font. Then, during initialization of the JComboBox, I call

fontComboBox.setSelectedItem(new Font(baseFont.getName(),baseFont.getStyle(),1)); 
//The size is 1 in all fonts retrieved from GraphicsEnvironment.
//Th开发者_如何学Pythone combo box contains objects of the type Font.

to set the selected font to the default system font.

This works most of the time. Although, it appears that for some fonts, the created base font does not match any of the fonts which are retrieved from GraphicsEnvironment.

For me, the base font is Dialog. The name of the font is "Dialog". However, the names for the fonts retrieved from GraphicsEnvironment for Dialog are "Dialog.Bold", "Dialog.Plain", and "Dialog.Italic". Since the created font has a different name, the combobox will not select the desired item (equals(...) fails).

Note how this works with most fonts (the only one I have tested so far that doesn't work is the Dialog font).

Is there a better solution to this that avoids this problem? Can I perhaps get the correct font names by using some specific other Swing component than JLabel to get the base font? Finally, why don't the names match?

EDIT: Followup question: How does one load a logical fonts physical font? (Making a JComboBox font chooser)


There are some fonts "Dialog", "Monospaced" and some more don't remember names aren't rel fonts. The artificial fonts are used by java but they are rendered with another physical font. It's done to provide platform independent font names.

It means e.g. for "Monospaced" font some OS dependent real font with equal chars' widhts is used. http://download.oracle.com/javase/1.3/docs/guide/intl/addingfonts.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜