What font in Swing looks the same in all OS?

I am using Netbeans 7.0 with JDK6 under Windows 7 to develop the user interface of my Java application. I apply the look of the system. But it looks the way I want it on Windows, but it differs from MacOS and, even worse, it looks different in different window managers on Linux (LXDE, GNOME, KDE, XFCE).

In different ways, I mean fonts and their size. On Windows, if the label looks like "v 1.23", it looks like "v ..." in other OSs, because the fonts are getting bigger in this OS, and JLabel does not have enough space to display. This happens in several places.

I do not want to increase the width of the label. I want the label to look the same at this given width in all OSs. By default, Netbeans uses the Tahoma 11pt font on my computer. I think it is not available in all OSs, so other OSs use a different font.

Is Arial a common font?

Should I change the font of each element to Arial manually? Or any other options?

+8
java user-interface swing netbeans
source share
4 answers

When it comes to window managers, it is much more than just the font size, for example, the width between two controls is handled differently in gnome and KDE. when it comes to using the same font size on all platforms, you can use the Sans Serif font, it is available in all the OSs I've worked with.

it would also be better (if you cannot find the serif in the OS where you want to run the application), you can download the font (free from GNU Free Fonts ).

when it comes to setting font sizes and fonts, why don't you try using a theme and setting the font there ... you can use the UIManager.setLookAndFeel() method to change the themes. here is a link to look and feel

+5
source share

Instead, use the logical font family . This Font example below adds a 24-point serif text to the center (default) of BorderLayout to achieve a nice result on disparate platforms.

 ta.setFont(new Font("Serif", Font.ITALIC, 24)); 

Mac OS X:

Mac

Windows 7:

Windows

Ubuntu Linux:

Linux

+8
source share

Lucida Sans Regular is always associated with Java, but I don’t think it will solve the problem that some texts / labels go out of format. It also depends on the resizing of the user's screen.

0
source share

Old question, but I found it because I was looking for a solution. And my solution: use DejaVu fonts. The Java dialog box font looks different on Linux and Windows, but DejaVuSans 12 is very similar to the dialog font on Linux and looks the same on Windows (at least on Windows 8.1). My code is:

 ... static Font dejaVuSans; static final String resPath = "<classpath>/resources/"; // no leading "/" ... public static void main(String[] args) { /* See: * https://stackoverflow.com/questions/7434845/setting-the-default-font-of-swing-program * https://stackoverflow.com/questions/8361947/how-to-get-getclass-getresource-from-a-static-context */ dejaVuSans = null; Font f; try { InputStream istream = <ClassName>.class.getClassLoader().getResourceAsStream( resPath + "DejaVuSans.ttf"); dejaVuSans = Font.createFont(Font.TRUETYPE_FONT, istream); f = dejaVuSans.deriveFont(Font.PLAIN, 12); } catch (Exception e) { System.err.println(e.getMessage()); f = null; } if (f == null) f = new Font("Dialog", Font.PLAIN, 12); java.util.Enumeration keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object value = UIManager.get (key); if (value != null && value instanceof javax.swing.plaf.FontUIResource) UIManager.put (key, f); } ... } 

Of course, if DejaVuSans.ttf is not embedded in the jar file, you can import it at runtime. See How to import a font? .

0
source share

All Articles