For swing applications, default font settings for components are in current theme and can be retrieved using UIManager:
public class JavaTesting {
public static void main(String[] args) {
System.out.println(UIManager.get("Label.font"));
}
}
This can be adjusted in JAVA_HOME/lib/swing.properties for range of applications with for example:
swing.defaultlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel
or set at command line with:
java -Dswing.defaultlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel MyApp
Or the application itself remembers it's look and feel and has this value stored somewhere in configuration (file). It works, because application can set look and feel for itself, e.g.:
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
This all looks nice, but applications need often smaller fonts (e.g. unimportant status bar message for Label) or larger fonts (Label with some heading). Is there any recommended way for this? As a user of high density monitor I had to discard lot of java applications, that use code like this - source code copied from squirrel sql:
Font tmp = (Font)UIManager.get("Label.font");
if (tmp != null) {
Font font = tmp.deriveFont(10.0f);
_statusBarFontInfo = new FontInfo(font);
}
This example code makes status bar completely unreadable, but most components are adjusted to new, bigger font.
For creating my own application, I might use some ratio (e.g. 50% to 150% of theme font), but hardcoding 0.50 - 1.50 range looks as a bad coding habbit as well. And it doesn't fix problem with applications that I don't have source code for. This belongs to the theme / L&F (Look & Feel, Look and Feel). Example:
FontUIResource Label_font_resource = (FontUIResource)javax.swing.UIManager.get("Label.font");
Font Label_font = Label_font_resource;
Font Label_font_bigger = Label_font.deriveFont(Label_font.getSize2D() * 1.5f);
It's worth asking before I try some ugly hacks like custom components replacing swing default ones, or Graphics2D.setFont adjustments, or any other scary stuff. :-)
Edit: the only thing that exists, is size variant, which is supported only by Nimbus theme. Allowed values are "mini", "small", "regular", "large". Example:
JComponent mini = new JButton("mini");
mini.putClientProperty("JComponent.sizeVariant", "mini");
However, looking into source code for Nimbus theme, it's not possible to simply adjust these values (e.g. scale to 150% for "large"), it's actually hard-coded!
package javax.swing.plaf.nimbus;
public final class NimbusStyle extends SynthStyle
{
public static final String LARGE_KEY = "large";
public static final String SMALL_KEY = "small";
public static final String MINI_KEY = "mini";
public static final double LARGE_SCALE = 1.15D;
public static final double SMALL_SCALE = 0.857D;
public static final double MINI_SCALE = 0.714D;
This value can be edited only by very advanced user (for example edit constant pool in JAVA_HOME/lib/rt.jar with program called "rej") - i tried it, and it works, BUT it doesn't work always, because they actually hard-coded (!!) the constants at several places (is this really best quality - standard library?), for example:
if ("large".equals(str))
{
this.scrollBarWidth = (int)(this.scrollBarWidth * 1.15D);
this.incrGap = (int)(this.incrGap * 1.15D);
this.decrGap = (int)(this.decrGap * 1.15D);
}
Bottom line: no, Java look and feel doesn't support that for now. I suggest using ratios, e.g. 0.7 - 1.3 of default font size.
JLabel smallLabel = new JLabel("some text");
smallLabel.setFont(smallLabel.getFont().deriveFont(0.8f * smallLabel.getFont().getSize2D()));
Instead of changing the Font
, Nimbus has a sizeVariant
for mini, small, regular and large components. This article has more examples: Resizing a Component.
myButton.putClientProperty("JComponent.sizeVariant", "mini");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With