Java Utililty Methods Swing Font Set

List of utility methods to do Swing Font Set

Description

The list of methods to do Swing Font Set are organized into topic(s).

Method

voidsetFont(@Nonnull final Font font)
Sets default Swing font.
final FontUIResource f = new FontUIResource(font);
final Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
    final Object key = keys.nextElement();
    final Object value = UIManager.get(key);
    if (value instanceof FontUIResource) {
        UIManager.put(key, f);
voidsetFont(Container container)
set Font
setFont(container, DEFAULT_FONT);
voidsetFont(Font font)
set Font
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
    Object key = keys.nextElement();
    Object value = UIManager.get(key);
    if (value instanceof FontUIResource) {
        UIManager.put(key, fontRes);
voidsetFont(HTMLDocument doc, Font font, Color fg)
Sets the default font for an HTML document (e.g., in a tool tip displaying HTML).
doc.getStyleSheet().addRule("body { font-family: " + font.getFamily() + "; font-size: " + font.getSize()
        + "pt" + "; color: " + getHexString(fg) + "; }");
voidsetFont(String fontName)
Changes the default UIManager font to the provided font.
String[] fieldList = { "Button.font", "CheckBox.font", "CheckBoxMenuItem.font", "ColorChooser.font",
        "ComboBox.font", "DesktopIcon.font", "EditorPane.font", "FormattedTextField.font", "Label.font",
        "List.font", "Menu.font", "MenuBar.font", "MenuItem.font", "OptionPane.font", "Panel.font",
        "PasswordField.font", "PopupMenu.font", "ProgressBar.font", "RadioButton.font",
        "RadioButtonMenuItem.font", "ScrollPane.font", "Slider.font", "Spinner.font", "TabbedPane.font",
        "Table.font", "TableHeader.font", "TextArea.font", "TextField.font", "TextPane.font",
        "TitledBorder.font", "ToggleButton.font", "ToolBar.font", "ToolTip.font", "Tree.font",
        "Viewport.font" };
...
voidsetFontRecursively(final JComponent component, final Font font, final boolean childsOnly)
Sets font of component and all of its children.
if (!childsOnly) {
    component.setFont(font);
for (int i = 0; i < component.getComponentCount(); i++) {
    if (component.getComponent(i) instanceof JComponent) {
        setFontRecursively((JComponent) component.getComponent(i), font, false);
voidsetFontScale(float scale)
set Font Scale
if (originals == null) {
    originals = new HashMap<>(25);
    for (Map.Entry entry : UIManager.getDefaults().entrySet()) {
        Object key = entry.getKey();
        if (key.toString().toLowerCase().contains(".font")) {
            Object value = entry.getValue();
            Font font = null;
            if (value instanceof Font) {
...
voidsetHtmlFont(HTMLDocument doc, Font font)
Sets the font used for HTML displays to the specified font.
String stylesheet = String.format(STYLESHEET, font.getName(), font.getSize(), font.getName(),
        font.getSize());
try {
    doc.getStyleSheet().loadRules(new StringReader(stylesheet), null);
} catch (IOException e) {
    throw new IllegalStateException(e);
voidsetItalicFont(JComponent jcomp)
set Italic Font
Font font = jcomp.getFont();
jcomp.setFont(new Font(font.getName(), Font.ITALIC, font.getSize()));
voidsetLabelProperties(JLabel label, Color color, Font font, Color bg)
set Label Properties
label.setVerticalTextPosition(JLabel.CENTER);
label.setHorizontalTextPosition(JLabel.CENTER);
if (bg != null) {
    label.setBackground(bg);
label.setOpaque(true);
if (font != null) {
    label.setFont(font);
...