Change Swing font size from command line

I use the Swing application, which displays text with a ridiculously small font size on my computer. I do not have access to the source code.

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


EDIT:

Small font sizes should no longer be a problem since Java 9. Swing began to scale its GUI components based on screen resolution .

+4
source share
3 answers

There is no command line switch to change the font size for Swing. You will need to call 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 do not have access to the source code, you can always write your own main wrapper class, where you call

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

before calling the main method of the actual application.

However, if the font size is very small, it will most likely be set explicitly, so changing the default values ​​may not help.

+2
source

I do not think that there are such parameters, since all font sizes of the component are defined in the default values ​​Look and Feel (L & F). Some of L & Fs allow you to quickly change fonts, some of them do not work. In most cases, you can change the font size by changing the default settings:

 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 } ) ); 

etc for each component.

And I'm not sure that these values ​​can be passed to the application without changing its code, or at least have some support for changing the font size inside the application.

+1
source

I have not tested it, but you can try to run the application. using Java Web Start . It allows you to specify properties such as swing.useSystemFontSettings and swing.metalTheme , even for sandboxed applications. Execution can either override the small fonts installed in the code.

See the JNLP file syntax for more details.

+1
source

Source: https://habr.com/ru/post/1434161/


All Articles