Java Utililty Methods Swing Font

List of utility methods to do Swing Font

Description

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

Method

JLabeladdLabel(Container component, String text, Icon icon, int horizontalAlignment, Font font)
Add a new label to a given component
JLabel label = new JLabel(text, icon, horizontalAlignment);
if (component != null) {
    component.add(label);
if (font != null) {
    label.setFont(font);
return label;
...
voidapplyComponentFont(JComponent c)
apply Component Font
c.setFont(guifont);
voidapplyFont(JPanel p)
apply Font
Component[] comps = p.getComponents();
for (Component c : comps) {
    c.setFont(guifont);
voidapplyProperties(final Component comp, final Color colBack, final Color colFore, final Font font)
Sets background and foreground color and a font for the specified component.
if (comp != null) {
    comp.setBackground(colBack);
    comp.setForeground(colFore);
    comp.setFont(font);
voidautoAwesomeLookAndFeel(String fontName, Map defaults)
auto Awesome Look And Feel
if (!isWindows()) {
    setSystemLookAndFeel();
if (UIManager.getLookAndFeel().toString().contains("MetalLookAndFeel")) {
    FontUIResource font = new FontUIResource(fontName == null ? Font.SANS_SERIF : fontName.trim(),
            Font.PLAIN, 12);
    java.util.Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
...
JComponentboldFont(JComponent component)
Updates a font in a component to be bold.
if (component == null)
    return null;
component.setFont(component.getFont().deriveFont(Font.BOLD));
return component;
StringclipText(JComponent c, Font fnt, String val, int xFrom, int xTo)
Clip a text.
FontMetrics fm;
String str;
int i, size, totWidth;
fm = c.getFontMetrics(fnt != null ? fnt : c.getFont());
totWidth = xFrom;
size = val.length();
for (i = 0; i < size; i++) {
    totWidth += fm.charWidth(val.charAt(i));
...
AttributeSetcreateTextAttributes(Font baseFont, Color color, boolean bold, boolean italic)
Creates text style attributes with the specified properties.
SimpleAttributeSet attrs = new SimpleAttributeSet();
if (baseFont != null) {
    StyleConstants.setFontFamily(attrs, baseFont.getFamily());
    StyleConstants.setFontSize(attrs, baseFont.getSize());
StyleConstants.setForeground(attrs, color);
StyleConstants.setBold(attrs, bold);
StyleConstants.setItalic(attrs, italic);
...
TextLayoutcreateTextLayout(JComponent c, String s, Font f, FontRenderContext frc)
create Text Layout
Object shaper = (c == null ? null : c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
if (shaper == null) {
    return new TextLayout(s, f, frc);
} else {
    Map<TextAttribute, Object> a = new HashMap<TextAttribute, Object>();
    a.put(TextAttribute.FONT, f);
    a.put(TextAttribute.NUMERIC_SHAPING, shaper);
    return new TextLayout(s, a, frc);
...
FontdefaultFont()
default Font
return (Font) UIManager.get("MenuItem.font");