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

RectangledrawCentredText(Graphics2D g, Font font, String text, int x, int y)
Draws the text by creating a rectange centered around x, y
g.setFont(font);
FontMetrics fm = g.getFontMetrics(font);
setPreferredAliasingMode(g);
Rectangle ret = new Rectangle(x, y, 0, 0);
if (text == null) {
    return ret;
String[] alines = text.split("\\n");
...
RectangledrawString(Graphics g, Font font, String text, Rectangle rect, int align)
draw String
return drawString(g, font, text, rect.x, rect.y, rect.width, rect.height, align);
voiddrawString(Graphics g, String text, int x, int y, Font font, Color color)
draw String
Graphics2D g2d = (Graphics2D) g;
RenderingHints oldHints = null;
Color oc = g2d.getColor();
Font of = g2d.getFont();
if (renderingHints != null) {
    oldHints = g2d.getRenderingHints();
    g2d.addRenderingHints(renderingHints);
if (font != null) {
    g2d.setFont(font);
if (color != null) {
    g2d.setColor(color);
g2d.drawString(text, x, y);
if (oldHints != null) {
    g2d.addRenderingHints(oldHints);
g2d.setFont(of);
g2d.setColor(oc);
FontgetAsNotUIResource(Font font)
Returns a Font based on the param which is not of type UIResource.
if (!(font instanceof UIResource))
    return font;
return font.deriveFont(font.getAttributes());
FontgetDefaultFont()
get Default Font
return new JLabel().getFont();
FontgetDefaultLabelBoldFont()
get Default Label Bold Font
return UIManager.getFont("Label.boldFont");
FontgetDefaultLabelFont()
get Default Label Font
Font ref = new JLabel().getFont();
return ref.deriveFont(Font.PLAIN, ref.getSize());
FontgetFont(String name)
get Font
return getFont(name, getSystemLAF());
ColorgetFontColorToUse(boolean isSelected)
get Font Color To Use
Color fontColorToUse;
if (isSelected) {
    fontColorToUse = Color.WHITE;
} else {
    if (LOOK_AND_FEEL_NAME.contains("Dark") || LOOK_AND_FEEL_NAME.equalsIgnoreCase("Darcula")) {
        fontColorToUse = FONT_COLOR_DARK;
    } else {
        fontColorToUse = FONT_COLOR_NORMAL;
...
JTextAreagetHelpTextArea(String help, Color background, Font font)
get Help Text Area
JTextArea helpText = new JTextArea(help);
helpText.setLineWrap(true);
helpText.setWrapStyleWord(true);
helpText.setBackground(background);
helpText.setFont(font);
helpText.setEditable(false);
return helpText;