Java Swing Font Set setFont(Container container)

Here you can find the source of setFont(Container container)

Description

set Font

License

Open Source License

Declaration

public static void setFont(Container container) 

Method Source Code


//package com.java2s;
/*/*w w  w .jav a 2  s .  co  m*/
 * Xapp (pronounced Zap!), A automatic gui tool for Java.
 * Copyright (C) 2009 David Webber. All Rights Reserved.
 *
 * The contents of this file may be used under the terms of the GNU Lesser
 * General Public License Version 2.1 or later.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class Main {
    public static Font DEFAULT_FONT = Font.decode("dialog-10");

    public static void setFont(Container container) {
        setFont(container, DEFAULT_FONT);
    }

    public static void setFont(Container container, String fontStr) {
        setFont(container, Font.decode(fontStr));
    }

    public static void setFont(Container container, Font font) {
        container.setFont(font);
        trySetBorderFont(font, container);
        Component[] components = container.getComponents();
        for (Component component : components) {
            component.setFont(font);
            if (component instanceof Container) {
                setFont((Container) component, font);
            }
            trySetBorderFont(font, component);
            if (component instanceof JTable) {
                JTable table = (JTable) component;
                table.getTableHeader().setFont(font);
            }
        }

        if (container instanceof JMenu) {
            JMenu jMenu = (JMenu) container;
            for (int i = 0; i < jMenu.getItemCount(); i++) {
                JMenuItem mi = jMenu.getItem(i);
                mi.setFont(font);
            }
        }
    }

    private static void trySetBorderFont(Font font, Component component) {
        if (component instanceof JComponent) {
            JComponent jc = (JComponent) component;
            if (jc.getBorder() instanceof TitledBorder) {
                TitledBorder titledBorder = (TitledBorder) jc.getBorder();
                titledBorder.setTitleFont(font);
            }
        }
    }
}

Related

  1. setComponentsFont(Container container, Font font)
  2. setDefaultFont(final Font font)
  3. setDefaultFont(Font font)
  4. setDefaultFont(java.awt.Font font)
  5. setFont(@Nonnull final Font font)
  6. setFont(Font font)
  7. setFont(HTMLDocument doc, Font font, Color fg)
  8. setFont(String fontName)
  9. setFontRecursively(final JComponent component, final Font font, final boolean childsOnly)