Reputation: 34840
I have a Java 7 Swing application developed in Windows XP. I use cross platform look and feel:
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
When I run it on linux (Fedora 12). The font is a little wider. So the layout changed in some places due to this.
Is there a way to make the application use the same font or similiar font which come with jre but not local system? then we can make its look and feel really the same.
thanks,
Upvotes: 4
Views: 3398
Reputation: 205875
It's not clear whether this is a font problem or a layout problem. Naturally, an sscce would help illustrate what you perceive to be the problem. By contrast, this example illustrates how the layout can adjust to accommodate the default font families on disparate platforms.
As you are using the cross-platform L&F, you might want to turn off bold fonts, as shown here. Instead of using an actual bold font, some platforms derive the bold style from a plain font, often with mixed results.
Upvotes: 1
Reputation: 347332
This may not work, but. You could provide your own Font, bundled with the application and load that at Runtime.
Check out Load fonts out of JAR-file and create AWT font (works) and register iText font (does not work) for an example (the answers not quite right, but the intention is good) or http://www.java2s.com/Code/Java/2D-Graphics-GUI/Loadfontfromttffile.htm
Once loaded, you could walk the UIManager's properties, replacing all keys with ".font" in there names to your Font instead.
The problem you are going to face ultimately is the difference in the rendering engines of the OS, including things like DPI and the difference in the Font rendering engines.
Upvotes: 1
Reputation: 17329
You could look at UIManager
. Swing uses the properties here, such as Label.font
, to get default fonts. So you could do:
Font font = // create font
UIManager.put("Label.font", font)
Make sure you change these before any components are created, or you'll get some with the correct font, others without. Here's a program that will show you the default properties that are in the UIManager
. Anything ending with .font
is what you're looking for.
Another approach would be to create a utility class that will create components with your own defaults:
public class MyComponents {
public static final Font LABEL_FONT = // create font
public static JLabel createLabel(String text) {
JLabel label = new JLabel(text);
label.setFont(LABEL_FONT);
return label;
}
}
If this is a new application without a lot of components, I recommend the second approach. If it's an old application with lots of component generation spread out everywhere, the first approach will be less time-consuming, but still the second approach would probably be better.
Upvotes: 4