Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Swing font size from command line

Tags:

java

swing

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.

like image 750
Lii Avatar asked Sep 14 '12 09:09

Lii


2 Answers

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.

like image 185
Ingo Kegel Avatar answered Oct 01 '22 02:10

Ingo Kegel


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.

like image 35
Mikle Garin Avatar answered Oct 01 '22 01:10

Mikle Garin