java swing minimal (or range for) font size for other application
Asked Answered
A

2

6

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()));
Alfonso answered 26/12, 2012 at 10:28 Comment(7)
nice question +1Kristankriste
Removed tag "Nimbus". This has nothing to do with theme "Nimbus", it was just an example. It concerns ALL themes.Alfonso
in Swing are Look and Feel, not themesKristankriste
Yes, but I like word "theme" more, I included (in the original post) all names "theme / L&F (Look & Feel, Look and Feel)". You obviously understood my comment with only word "theme" anyway.Alfonso
only slightly related: as a general rule, applications should respect the user's font setting (instead of second-guessing by f.i. trying to calculate sizes by dpi) Unfortunately, Swing isn't overly well-behaved in that area.Philippa
Thanks for link, yes, respecting user's font is good idea. Nevertheless, the question still stands - how big or small should it be, compared to user's font settings, in case larger or smaller font is needed?Alfonso
I added "nimbus" tag in the end anyway, because it's actually the only theme, that tries (very stupid way, and unsuccessfully) to solve the problem.Alfonso
A
1

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");
Adina answered 26/12, 2012 at 15:57 Comment(0)
S
0

What I've done in my Swing applications is include a Font class. Every other Swing component that needs a font calls the Font class.

import java.awt.Font;

public class MinesweeperFont {

    protected static final String FONT_NAME = "Comic Sans MS";

    public static Font getBoldFont(int pointSize) {
        return new Font(FONT_NAME, Font.BOLD, pointSize);
    }

}

Now in this case, I only needed one font type. I let the application pick the font size.

You could define a method for each font size you want, probably calling them small, medium, and large, so you can change the methods later to use a different point size or type if you want.

Since your entire application calls these font static methods, you only have to make font changes in one place.

Stegosaur answered 26/12, 2012 at 14:7 Comment(1)
I am describing your solution in my question as unsatisfactory: see the bold "ratio" part. I am asking if it can be done BETTER - to share settings among Java applications (like "Look and feel"), and/or how to fix it for existing Java applications that use absolute font size, not proportional.Alfonso

© 2022 - 2024 — McMap. All rights reserved.