Java Swing Font Change changeFont(JComponent comp, double scaleFactor, int style)

Here you can find the source of changeFont(JComponent comp, double scaleFactor, int style)

Description

Scale the original font of a component by a given factor and change the style

License

Apache License

Parameter

Parameter Description
comp the component for which the font has to be changed
scaleFactor the scale factor (the result will be rounded to an integer)
style the new style

Declaration

public static void changeFont(JComponent comp, double scaleFactor,
        int style) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Font;
import javax.swing.JComponent;

public class Main {
    /**/*w  w  w  .j ava 2s . c o  m*/
     * Scale the original font of a component by a given factor
     * 
     * @param comp
     *          the component for which the font has to be changed
     * @param scaleFactor
     *          the scale factor (the result will be rounded to an integer)
     */
    public static void changeFont(JComponent comp, double scaleFactor) {
        Font font = comp.getFont();
        comp.setFont(scale(font, scaleFactor));
    }

    /**
     * Change the font style of a component
     * 
     * @param comp
     *          the component for which the font has to be changed
     * @param style
     *          the new style
     */
    public static void changeFont(JComponent comp, int style) {
        Font font = comp.getFont();
        comp.setFont(font.deriveFont(style));
    }

    /**
     * Scale the original font of a component by a given factor and change the style
     * 
     * @param comp
     *          the component for which the font has to be changed
     * @param scaleFactor
     *          the scale factor (the result will be rounded to an integer)
     * @param style
     *          the new style
     */
    public static void changeFont(JComponent comp, double scaleFactor,
            int style) {
        Font font = comp.getFont();
        font = scale(font, scaleFactor);
        comp.setFont(font.deriveFont(style));
    }

    private static Font scale(Font font, double factor) {
        int newSize = Math.round((float) (font.getSize() * factor));
        return font.deriveFont((float) newSize);
    }
}

Related

  1. changeDefaultFontSize(int fontSize)
  2. ChangeFont(JComponent comp, int wheel_rotation)
  3. changeFontSize(final int size)
  4. changeFontSize(Font font, float factor)
  5. changeFontStyle(Font font, int style)