Java Swing Font Change changeFontSize(final int size)

Here you can find the source of changeFontSize(final int size)

Description

change Font Size

License

Open Source License

Declaration

@SuppressWarnings("ConstantConditions")
    public static void changeFontSize(final int size) 

Method Source Code


//package com.java2s;
/*// w  ww.  j a v  a  2  s .c  o  m
 * jGnash, a personal finance application
 * Copyright (C) 2001-2015 Craig Cavanaugh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Font;

import java.awt.Insets;

import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

public class Main {
    private static float growthPercentage = 1f;

    @SuppressWarnings("ConstantConditions")
    public static void changeFontSize(final int size) {

        // get UI defaults
        UIDefaults defaults = UIManager.getLookAndFeelDefaults();

        { // determine percent reduction for insets, etc.
            float baseSize = getBaseFontSize();

            growthPercentage = size / baseSize;
        }

        // reduce font sizes
        defaults.keySet().stream().filter(keyObj -> keyObj instanceof String).forEach(keyObj -> {
            String key = (String) keyObj;

            if (key.contains("font")) {

                Object object = defaults.get(key);

                if (object instanceof Font) {
                    Font font = (Font) object;

                    Font derived = font.deriveFont((float) size);

                    defaults.put(key, derived);

                } else if (object instanceof FontUIResource) {
                    FontUIResource resource = (FontUIResource) object;

                    FontUIResource derived = new FontUIResource(resource.deriveFont((float) size));
                    defaults.put(key, derived);
                }
            }
        });

        // reduce content Margins
        defaults.keySet().stream().filter(keyObj -> keyObj instanceof String).forEach(keyObj -> {
            String key = (String) keyObj;

            if (key.contains("contentMargins") || key.contains("padding")) {

                Insets derived = (Insets) ((Insets) defaults.get(key)).clone();

                if (derived.left > 0) {
                    derived.left = (int) Math.ceil(derived.left * growthPercentage);
                }

                if (derived.right > 0) {
                    derived.right = (int) Math.ceil(derived.right * growthPercentage);
                }

                if (derived.top > 0) {
                    derived.top = (int) Math.ceil(derived.top * growthPercentage);
                }

                if (derived.bottom > 0) {
                    derived.bottom = (int) Math.ceil(derived.bottom * growthPercentage);
                }

                defaults.put(key, derived);
            }
        });

        // reduce content Margins
        defaults.keySet().stream().filter(keyObj -> keyObj instanceof String).forEach(keyObj -> {
            String key = (String) keyObj;

            if (key.contains("textIconGap") || key.contains("size") || key.contains("thumbWidth")
                    || key.contains("thumbHeight")) {

                Integer integer = (Integer) defaults.get(key);
                Integer derived = (int) Math.ceil((float) integer * growthPercentage);

                defaults.put(key, derived);
            }
        });
    }

    /**
     * Returns the base font size for the Nimbus Look and Feel
     * <p/>
     * Assumes that the Nimbus look and feel has been set.
     * 
     * @return base base font size
     */
    @SuppressWarnings("ConstantConditions")
    public static int getBaseFontSize() {

        int baseSize = 12;

        UIDefaults defaults = UIManager.getLookAndFeelDefaults();

        Object object = defaults.get("Label.font");

        if (object != null) {
            if (object instanceof Font) {
                baseSize = ((Font) object).getSize();
            } else if (object instanceof FontUIResource) {
                baseSize = ((FontUIResource) object).getSize();
            }
        }

        return baseSize;
    }
}

Related

  1. changeDefaultFontSize(int fontSize)
  2. changeFont(JComponent comp, double scaleFactor, int style)
  3. ChangeFont(JComponent comp, int wheel_rotation)
  4. changeFontSize(Font font, float factor)
  5. changeFontStyle(Font font, int style)
  6. changeFontToItalic(final JComponent component)
  7. installFont(Component c, Font font)