6

I am using a Swing application which on my computer shows text with a ridiculously small font size.

Is there a way to change the font size, or maybe some kind of DPI setting, from the command line or with some kind of configuration file (for example, something like a swing.properties file)?

I don't have access to the source code.


EDIT:

Small font sizes should not be a problem any more since Java 9. Swing has started to scale its GUI components depending on the screen resolution.

Lii
  • 11,553
  • 8
  • 64
  • 88

3 Answers3

2

There is no command line switch to change the font size for Swing. What you would have to do is to invoke the following method:

public static void adjustFontSize(int adjustment) {

    UIDefaults defaults = UIManager.getDefaults();
    List<Object> newDefaults = new ArrayList<Object>();
    Map<Object, Font> newFonts = new HashMap<Object, Font>();

    Enumeration<Object> en = defaults.keys();
    while (en.hasMoreElements()) {
        Object key = en.nextElement();
        Object value = defaults.get(key);
        if (value instanceof Font) {
            Font oldFont = (Font)value;
            Font newFont = newFonts.get(oldFont);
            if (newFont == null) {
                newFont = new Font(oldFont.getName(), oldFont.getStyle(), oldFont.getSize() + adjustment);
                newFonts.put(oldFont, newFont);
            }
            newDefaults.add(key);
            newDefaults.add(newFont);
        }
    }
    defaults.putDefaults(newDefaults.toArray());
}

where adjustment is the number of points that should be added to each font size.

If you don't have access to the source code, you can always write your own wrapper main class where you call

    UIManager.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getPropertyName().equals("lookAndFeel")) {
                adjustFontSize(5);
            }
        }
    });

before invoking the main method of the actual application.

However, if the font size is very small, it has likely been set explicitly, so changing the defaults might not help.

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
  • The wrapper main class is a nice trick, I'll try that and see how it works out. – Lii Sep 14 '12 at 13:17
1

I don't think there are such options since all the component Font sizes are defined in Look and Feel (L&F) default values. Some of L&Fs allow quick font changes, some of them doesn't. In most cases you should be able change the font size by changing the UI defaults:

UIManager.put ( "Button.font", new SwingLazyValue ( "javax.swing.plaf.FontUIResource", null, new Object[]{ fontName, fontStyle, fontSize } ) );
UIManager.put ( "Label.font", new SwingLazyValue ( "javax.swing.plaf.FontUIResource", null, new Object[]{ fontName, fontStyle, fontSize } ) );
UIManager.put ( "TextField.font", new SwingLazyValue ( "javax.swing.plaf.FontUIResource", null, new Object[]{ fontName, fontStyle, fontSize } ) );

e.t.c for each component.

And i am not sure if those values could be passed to application without changing its code or atleast having some font-size change support inside the application.

Mikle Garin
  • 10,083
  • 37
  • 59
  • This seems to be the same approach as in Ingo Kegel's suggestion, which didn't work. – Lii Oct 01 '12 at 21:32
  • 1
    @Lii it will work only with L&F that supports fonts from UI defaults. And to know if your L&F supports them - you will have to digg its source code... – Mikle Garin Oct 02 '12 at 08:21
1

I have not tested it, but you might try launching the app. using Java Web Start. It allows properties like swing.useSystemFontSettings & swing.metalTheme to be specified even for sand-boxed apps. Doing either might 'override' small fonts set in the code.

See the JNLP file syntax for more details.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for the suggestion, but this is to much effort to be worth it in this case. – Lii Oct 01 '12 at 21:29