Java Swing Font applyProperties(final Component comp, final Color colBack, final Color colFore, final Font font)

Here you can find the source of applyProperties(final Component comp, final Color colBack, final Color colFore, final Font font)

Description

Sets background and foreground color and a font for the specified component.

License

Open Source License

Parameter

Parameter Description
comp Component to set properties for.
colBack Background color. Can be <code>null</code> to use parent color (not suggested due to differences between Java VMs).
colFore Foreground color. Can be <code>null</code> to use parent color (not suggested due to differences between Java VMs).
font Font. Can be <code>null</code> to use parent font (not suggested due to differences between Java VMs).

Declaration

public static void applyProperties(final Component comp, final Color colBack, final Color colFore,
        final Font font) 

Method Source Code

//package com.java2s;
/*/* ww  w.  ja  va 2 s .c o m*/
 * ------------------------------------------------------------------
 * This source code, its documentation and all appendant files
 * are protected by copyright law. All rights reserved.
 *
 * Copyright (C) 2012
 * Novartis Institutes for BioMedical Research
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, Version 3, as
 *  published by the Free Software Foundation.
 *
 *  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>.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  KNIME interoperates with ECLIPSE solely via ECLIPSE's plug-in APIs.
 *  Hence, KNIME and ECLIPSE are both independent programs and are not
 *  derived from each other. Should, however, the interpretation of the
 *  GNU GPL Version 3 ("License") under any applicable laws result in
 *  KNIME and ECLIPSE being a combined program, KNIME GMBH herewith grants
 *  you the additional permission to use and propagate KNIME together with
 *  ECLIPSE with only the license terms in place for ECLIPSE applying to
 *  ECLIPSE and the GNU GPL Version 3 applying for KNIME, provided the
 *  license terms of ECLIPSE themselves allow for the respective use and
 *  propagation of ECLIPSE together with KNIME.
 *
 *  Additional permission relating to nodes for KNIME that extend the Node
 *  Extension (and in particular that are based on subclasses of NodeModel,
 *  NodeDialog, and NodeView) and that only interoperate with KNIME through
 *  standard APIs ("Nodes"):
 *  Nodes are deemed to be separate and independent programs and to not be
 *  covered works.  Notwithstanding anything to the contrary in the
 *  License, the License does not apply to Nodes, you are not required to
 *  license Nodes under the License, and you are granted a license to
 *  prepare and propagate Nodes, in each case even if such Nodes are
 *  propagated with or for interoperation with KNIME.  The owner of a Node
 *  may freely choose the license terms applicable to such Node, including
 *  when such Node is propagated with or for interoperation with KNIME.
 * ---------------------------------------------------------------------
 */

import java.awt.Color;
import java.awt.Component;

import java.awt.Font;

import javax.swing.UIManager;

public class Main {
    /**
     * Defines the default fall back color for backgrounds. The default is
     * white.
     */
    private static final Color DEFAULT_BACKGROUND = Color.white;
    /**
     * Defines the default fall back color for foregrounds. The default is
     * black.
     */
    private static final Color DEFAULT_FOREGROUND = Color.black;
    /**
     * Defines the default fall back font to be used when no other font is
     * available. The default is Font("Helvetica", Font.PLAIN, 12).
     */
    private static Font g_defaultFont = null;

    /**
     * Sets background and foreground color and a font for the specified
     * component.
     * 
     * @param comp
     *            Component to set properties for.
     * @param colBack
     *            Background color. Can be <code>null</code> to use parent color
     *            (not suggested due to differences between Java VMs).
     * @param colFore
     *            Foreground color. Can be <code>null</code> to use parent color
     *            (not suggested due to differences between Java VMs).
     * @param font
     *            Font. Can be <code>null</code> to use parent font (not
     *            suggested due to differences between Java VMs).
     */
    public static void applyProperties(final Component comp, final Color colBack, final Color colFore,
            final Font font) {
        if (comp != null) {
            comp.setBackground(colBack);
            comp.setForeground(colFore);
            comp.setFont(font);
        }
    }

    /**
     * Sets background and foreground color and a font for the specified
     * component.
     * 
     * @param comp
     *            Component to set properties for. If <code>
     * null</code> nothing happens.
     * @param strKeyBackground
     *            Key for background color defined in
     *            <code>javax.swing.UIManager</code>. If it is <code>null</code>
     *            or not found first the key "control" will be tried, and if
     *            still <code>null</code>, a "fallback" color will be taken.
     * @param strKeyForeground
     *            Key for background color defined in
     *            <code>javax.swing.UIManager</code>. If it is <code>null</code>
     *            or not found first the key "textText" will be tried, and if
     *            still <code>null</code>, a "fallback" color will be taken.
     * @param strKeyFont
     *            Key for font defined in <code>
     * javax.swing.UIManager</code>. If it is <code>null</code> or not found
     *            first the key "Label.font" will be tried, and if still
     *            <code>null</code>, a "fallback" font will be taken.
     */
    public static void applyProperties(final Component comp, final String strKeyBackground,
            final String strKeyForeground, final String strKeyFont) {
        if (comp != null) {
            Color colBack = null;
            Color colFore = null;
            Font font = null;

            if (strKeyBackground != null) {
                colBack = UIManager.getColor(strKeyBackground);
            }

            if (strKeyForeground != null) {
                colFore = UIManager.getColor(strKeyForeground);
            }

            if (strKeyFont != null) {
                font = UIManager.getFont(strKeyFont);
            }

            if (colBack == null) {
                colBack = UIManager.getColor("control"); // $NON-NLS-1$

                if (colBack == null) {
                    colBack = DEFAULT_BACKGROUND; // Fall back default
                }
            }

            if (colFore == null) {
                colFore = UIManager.getColor("controlText"); // $NON-NLS-1$

                if (colFore == null) {
                    colFore = DEFAULT_FOREGROUND; // Fall back default
                }
            }

            if (font == null) {
                font = UIManager.getFont("Label.font"); // $NON-NLS-1$

                if (font == null) {
                    font = g_defaultFont; // Fall back default
                }
            }

            applyProperties(comp, colBack, colFore, font);
        } // end if
    }
}

Related

  1. addLabel(Container component, String text, Icon icon, int horizontalAlignment, Font font)
  2. applyComponentFont(JComponent c)
  3. applyFont(JPanel p)
  4. autoAwesomeLookAndFeel(String fontName, Map defaults)
  5. boldFont(JComponent component)
  6. clipText(JComponent c, Font fnt, String val, int xFrom, int xTo)
  7. createTextAttributes(Font baseFont, Color color, boolean bold, boolean italic)