Changing font size at run time in a Swing application

Currently, many websites have the ability to allow the user to increase or decrease the size of the font that appears on the site. I would like to add similar features to my Java Swing application, with the ability to change the font either to a larger size or to return to standard sizes. The user will be able to do this at run time through a menu item.

The current version of my code does not work very well - the code iterates over the keys stored in UIManager / UIDefaults, and for each key that is a font, my code receives a new font with the size of the target point (for example, if it is larger, then the new point size usually equal to 16, otherwise if you are going to a regular size, usually 11), and then puts this key and the new font in the UIManager. The code then calls SwingUtilities.updateComponentTreeUI () in the frame, and then packs the frame.

The code will increase the font once, but returning to the normal size is not affected. This is especially noticeable in the menu bar.

Am I going to do it right? Should I instead create a new look with a large dot size or make full use of some other method?

Thanks in advance for your help.

+4
source share
1 answer

The code will enlarge the font once, but returning to regular size has no effect

I believe you need to update the UIManager with FontUIResource, not just with the font.

+4
source

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


All Articles